使用lerna 的import 我们可以方便的将一个普通的npm 包倒入到lerna 管理的monorepo 中

环境准备

  • lerna init
    注意必须是一个git 项目,同时需要commit ,不然会报错
lerna init
  • 创建一个npm package
    不再lerna 初始化的目录, 注意必须是一个git 项目,同时需要commit
 
yarn init -y
index.js:
module.exports = {
  username:"dalong",
  age:333
}
  • 目录结构如下
├── platform
│ ├── lerna.json
│ ├── package.json
│ └── packages
└── userlogin
    ├── index.js
    └── package.json

使用import

  • import
    如何userlogin 不是git 项目,会有如下的错误提示,注意需要git init 并提交
 
 cli v3.8.0
lerna ERR! Error: Command failed: git log --format=%h
lerna ERR! fatal: Not a git repository (or any of the parent directories): .git
lerna ERR! 
lerna ERR! 
lerna ERR! at makeError (/usr/local/lib/node_modules/lerna/node_modules/execa/index.js:174:9)
lerna ERR! at Function.module.exports.sync (/usr/local/lib/node_modules/lerna/node_modules/execa/index.js:338:15)
lerna ERR! at Object.execSync (/usr/local/lib/node_modules/lerna/node_modules/@lerna/child-process/index.js:22:16)
lerna ERR! at ImportCommand.externalExecSync (/usr/local/lib/node_modules/lerna/node_modules/@lerna/import/index.js:137:34)
lerna ERR! at ImportCommand.initialize (/usr/local/lib/node_modules/lerna/node_modules/@lerna/import/index.js:82:25)
lerna ERR! at Promise.resolve.then (/usr/local/lib/node_modules/lerna/node_modules/@lerna/command/index.js:266:24)
lerna ERR! at <anonymous>
lerna ERR! lerna Command failed: git log --format=%h
lerna ERR! lerna fatal: Not a git repository (or any of the parent directories): .git
lerna ERR! lerna 
 

如何lerna 项目没有commit 会有如下提示,lerna 项目也必须完整的commit

Error: Command failed: git rev-parse HEAD
lerna ERR! fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
lerna ERR! Use '--' to separate paths from revisions, like this:
lerna ERR! 'git <command> [<revision>...] -- [<file>...]'
lerna ERR! 
lerna ERR! HEAD
lerna ERR! 
lerna ERR! at makeError (/usr/local/lib/node_modules/lerna/node_modules/execa/index.js:174:9)
lerna ERR! at Function.module.exports.sync (/usr/local/lib/node_modules/lerna/node_modules/execa/index.js:338:15)
lerna ERR! at Object.execSync (/usr/local/lib/node_modules/lerna/node_modules/@lerna/child-process/index.js:22:16)
lerna ERR! at ImportCommand.getCurrentSHA (/usr/local/lib/node_modules/lerna/node_modules/@lerna/import/index.js:129:34)
lerna ERR! at ImportCommand.initialize (/usr/local/lib/node_modules/lerna/node_modules/@lerna/import/index.js:98:31)
lerna ERR! at Promise.resolve.then (/usr/local/lib/node_modules/lerna/node_modules/@lerna/command/index.js:266:24)
lerna ERR! at <anonymous>
lerna ERR! lerna Command failed: git rev-parse HEAD
lerna ERR! lerna fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
lerna ERR! lerna Use '--' to separate paths from revisions, like this:
lerna ERR! lerna 'git <command> [<revision>...] -- [<file>...]'
lerna ERR! lerna 
lerna ERR! lerna HEAD
 

import 提示信息,同意即可

lerna import ../userlogin         
lerna notice cli v3.8.0
lerna info About to import 1 commits from ../userlogin into packages/userlogin
? Are you sure you want to import these commits onto the current branch? (ynH) 

信息如下:

lerna notice cli v3.8.0
lerna info About to import 1 commits from ../userlogin into packages/userlogin
? Are you sure you want to import these commits onto the current branch? Yes
lerna info 8a94131 
lerna success import finished
 
 
  • 效果
├── lerna.json
├── package.json
└── packages
    └── userlogin
        ├── index.js
        └── package.json
  
  • 使用倒入的package
    在platform 目录,按照提示操作即可
 
lerna create loginui 
cd loginui 
lerna add userlogin
  • 编写调用代码
loginui/lib/loginui.js
'use strict';
const userlogin = require("userlogin");
module.exports = loginui;
function loginui() {  
    return userlogin;
}
简单测试代码
loginui/__tests__/loginui.test.js
```code
'use strict';
const loginui = require('..');
console.log(loginui())
 

添加npm script
packages/loginui/package.json

 
+"scripts": {
+ "test": "node __tests__/loginui.test"
+}
  • 在lerna 根项目编写npm script 调用测试
    platform/package.json
 
+ "scripts": {
+ "test":"lerna run test"
+ }
  • 调用效果
    yarn test
 
yarn test
yarn run v1.10.1
$ lerna run test
lerna notice cli v3.8.0
lerna info Executing command in 1 package: "npm run test"
lerna info run Ran npm script 'test' in 'loginui' in 0.5s:
> loginui@0.0.0 test /Users/dalong/mylearning/lerna-project/dalongdemo/platform/packages/loginui
> node __tests__/loginui.test
{ username: 'dalong', age: 333 }
lerna success run Ran npm script 'test' in 1 package in 0.5s:
lerna success - loginui
✨ Done in 1.33s.
 

说明

lerna 对于我们确认大型项目配置的一致性有很大的帮助

参考资料

https://lernajs.io/#command-import

lerna import && add 使用&&常见问题解决的更多相关文章

  1. 接口测试之——Charles抓包及常见问题解决(转载自https://www.jianshu.com/p/831c0114179f)

    简介 Charles其实是一款代理服务器,通过成为电脑或者浏览器的代理,然后截取请求和请求结果达到分析抓包的目的.该软件是用Java写的,能够在Windows,Mac,Linux上使用,安装Charl ...

  2. springmvc环境搭建以及常见问题解决

    1.新建maven工程 a)  打开eclipse,file->new->project->Maven->Maven Project b)  下一步 c)   选择创建的工程为 ...

  3. OpenStack安装部署管理中常见问题解决方法

    一.网络问题-network 更多网络原理机制可以参考<OpenStack云平台的网络模式及其工作机制>. 1.1.控制节点与网络控制器区别 OpenStack平台中有两种类型的物理节点, ...

  4. C# .Net Framework4.5中配置和使用managedCUDA及常见问题解决办法

    主要参考英文帖子.我就不翻译了哈.很容易懂的. 先说明我的运行平台: 1.IDE:Visual Studio 2012 C# .Net Framework4.5,使用默认安装路径: 2.显卡类型:NV ...

  5. ubuntu安装navicat及常见问题解决

    1.安装navicat Step1: 下载Navicat ,网址:http://www.navicat.com/en/download/download.html Step2:进入下载目录,解压压缩包 ...

  6. Web Deploy发布网站及常见问题解决方法(图文)

    Web Deploy发布网站及常见问题解决方法(图文) Windows2008R2+IIs7.5 +Web Deploy 3.5 Web Deploy 3.5下载安装 http://www.iis.n ...

  7. charles抓包的安装,使用说明以及常见问题解决(windows)

    charles抓包的安装,使用说明以及常见问题解决(windows) https://blog.csdn.net/zhangxiang_1102/article/details/77855548

  8. Linux运维常见问题解决集锦【转】

    作为linux运维,多多少少会碰见这样那样的问题或故障,用点心,平时多注意积累,水平肯定越来越高. 下面就是常见问题解决集锦:   1.shell脚本不执行 问题:某天研发某同事找我说帮他看看他写的s ...

  9. Python爬虫编程常见问题解决方法

    Python爬虫编程常见问题解决方法: 1.通用的解决方案: [按住Ctrl键不送松],同时用鼠标点击[方法名],查看文档 2.TypeError: POST data should be bytes ...

随机推荐

  1. vue.set动态新增对象属性,触发dom渲染

    当我们给一个props或者data中被观测的对象添加一个新的属性的时候,不能直接添加,必须使用Vue.set方法 /** * ==== 选择产品 ==== * 因为vue实现双向数据绑定的机制是数据劫 ...

  2. C/C++知识补充 (1)

    ● C++的圆括号运算符() 下列关于圆括号运算符的功能说法不正确的是(C) . A. 可用于强制类型转换 B 可用于类型构造 C 可用于类型声明 D 可用于函数调用 对大部分可重载的运算符来说,它既 ...

  3. Cracking The Coding Interview 1.7

    //Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set ...

  4. nopcommerce 4.1 net core 学习1

    关注nop有2年多, 基本都是下载看看结构, 里面的多语言跟主题模板 插件加载 是我比较喜欢的. 最近看nop4.1版本已经全面替换成了asp.net core 2.1 +ef core2.1,所以专 ...

  5. DevExpress WinForms v18.2新版亮点(五)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v1 ...

  6. 谷歌开源的TensorFlow Object Detection API视频物体识别系统实现(二)[超详细教程] ubuntu16.04版本

    本节对应谷歌开源Tensorflow Object Detection API物体识别系统 Quick Start步骤(一): Quick Start: Jupyter notebook for of ...

  7. Problem C: 平面上的点——Point类 (III)

    Description 在数学上,平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定.现在我们封装一个“Point类”来实现平面上的点的操作. 根据“append.cc”,完成Point类的构造方 ...

  8. Eclipse下SpringBoot没有自动加载application.properties文件

    Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...

  9. SpringMvc使用FastJson做为json的转换器(注解方式)

    在使用XML方式配置项目,使用fastjson做为Json转换器时通常的在XML内添加如下的配置: <mvc:message-converters register-defaults=" ...

  10. 在使用MyCat和MySqL时的错误总结

    在mysql中,无法连接虚拟机中的mysql. 原因:防火墙没有关闭 解决方案:service iptables stop 在mycat中,无法打开数据库的表, 原因:mycat在配置文件中设置的是自 ...