notes

使用verdaccio搭建私有库

docker pull verdaccio/verdaccio
docker run -d --name verdaccio -p 4873:4873 verdaccio/verdaccio

verdaccio使用文档

私有库搭建好后创建账号和发布

npm set registry http://server_ip:4873
npm adduser --registry http://server_ip:4873
npm publish --registry http://server_ip:4873

或使用nrm管理源

# 安装nrm
npm install -g nrm
# 添加本地的npm镜像地址
nrm add name http://XXXXXX:4873
# 使用本址的镜像地址 name为你要增加的地址
nrm use name 
# 查看本地所有源
nrm ls
# 查看当前源
npm config ls

npm私有库项目配置

// package.json
{
  "name": "@lianluo/js-call-native",
  "version": "0.1.0",
  "description": "js call native",
  "author": "hqq",
  "license": "MIT",
  "homepage": "https://mops-gitlab.lianluo.com/js-component/js-call-native",
  "repository": {
    "type": "git",
    "url": "git@mops-gitlab.lianluo.com:js-component/js-call-native.git"
  },
  "bugs": {
    "url": "https://mops-gitlab.lianluo.com/js-component/js-call-native/issues"
  },
  "main": "dist/jscallnative.min.js",
  "dependencies": {},
  "devDependencies": {
    "babel-core": "^6.7.7",
    "babel-eslint": "^8.2.2",
    "babel-loader": "^6.2.4",
    "babel-plugin-add-module-exports": "^0.1.4",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-stage-0": "^6.24.1",
    "eslint": "^4.17.0",
    "eslint-plugin-react": "^7.6.1",
    "webpack": "~1.12.11"
  },
  "scripts": {
    "dist": "webpack"
  }
}
name和version

这两个属性是必须要有的,否则模块就无法被安装,这两个属性一起形成了一个npm模块的唯一标识符

repository

指定一个代码存放地址,对想要为你的项目贡献代码的人有帮助

bugs

填写一个bug提交地址或者一个邮箱,被你的模块坑到的人可以通过这里吐槽

main

main属性指定了程序的主入口文件

// webpack.config.js
module.exports = {
    entry: {
        jscallnative : './src/JsCallNative.js'
    },
    output: {
        path: './dist',
        filename: '[name].min.js',
        libraryTarget: 'umd'
    },
    module: {
        loaders: [
            {
                test: /\.js$/, loader: 'babel'
            }
        ]
    }
};
entry

用来定义入口文件

output

output参数是个对象,用于定义构建后的文件的输出。 libraryTarget是导出方式,umd方式是支持CMD,AMD和全局window的访问方式。

module

关于模块的加载相关,我们就定义在module.loaders中。这里通过正则表达式去匹配不同后缀的文件名,然后给它们定义不同的加载器。比如给js文件定义babel加载器。

// .babelrc
{
    "presets": [
      "es2015",
      "stage-0"
    ],
    "plugins": ["add-module-exports"]
}
presets

presets字段是用来设定转码规则

关于scope(作用域)

执行npm install会从默认的registry去拉取,如果在dependencies里面依赖了私有库和公有库的话,拉取私有库可能会报模块找不到的错误。这个时候就需要用到scope。 在上面的package.jsonname字段里,有@lianluo的前缀,私有库的命名都要加上,一个作用是保证模块名不会和官方的冲突,还有一个作用是告诉npm这个是私有库,需要从另一个地址拉取。

私有库从另一个地址拉取的配置是在项目根目录建立一个.npmrc文件写入如下的东西。 npmrc文件的作用

registry=https://registry.npm.taobao.org/
@lianluo:registry=http://server_ip
//server_ip/:_authToken="xxxxxxxxxxxxxxxxxxxxxxxx"

第一行指定默认的registry。 第二行就是说@lianluo开头的私有库从http://server_ip地址拉取。 第三行保存用户登录信息,只有用户登录后才有。

发布私有库

如果项目根目录有.npmrc文件,里面有用户的登录信息。 那么只要在项目根目录下执行

npm publish --registry http://server_ip

如果用户未登录,则先登录

npm login --registry http://server_ip

登录后的用户信息保存在~/.npmrc路径下,然后把里面的用户信息复制到项目根目录下即可。 一般registry指向官方的源或淘宝的源,所以发布模块的时候一般带上--registry参数。

集成到gitlab-CI

// .gitlab-ci.yml
image: zacksleo/node

stages:
  - install_deps
  - publish

install_deps:
  stage: install_deps
  when: manual
  cache:
    key: ${CI_BUILD_REF_NAME}
    paths:
      - node_modules/
  script:
    - npm install

npm_publish:
  stage: publish
  variables:
    PUBLISH_SERVER: "SERVER_IP"
  dependencies: []
  cache:
    key: "$CI_BUILD_REF_NAME"
    policy: pull
    paths:
      - node_modules
  script:
    - if [ ! -d "node_modules" ]; then
    - npm install
    - fi
    - yarn eslint ./
    - npm run dist
    - npm publish --registry http://$PUBLISH_SERVER
  only:
    - tags
  tags:
    - mainland

注意:CI文件中的npm publish --registry http://$PUBLISH_SERVER这行命令需要在项目根目录下有.npmrc文件,且里面有用户的登录信息,才会执行成功。

遇到的一些问题


如果私有库版本有更新,执行npm update更新不了私有库,需要在package.json里面把那个私有库先去掉,再执行以下命令才会安装最新版本。

npm install --save @lianluo/js-call-native

verdaccio/verdaccio镜像创建的容器,使用挂载命令挂载外部文件后发布私有模块报错。 解决办法是使用别的镜像,这里使用fengcen/verdaccio

docker run -d --name verdaccio -v /mnt/data/verdaccio:/verdaccio -p 4873:4873 fengcen/verdaccio

执行npm adduser 和 npm login之后依然在.npmrc文件中获取不到authtoken的问题