5分鐘內3種方法搭建企業內部私有npm倉庫

下面經過三種方法來搭建公司私有npm倉庫,每種方式都有本身的優點。node

Node.js >= 6.11.3,個人Node版本:node v8.2.1
Linux or OSX,個人系統版本:CentOS Linux release 7.2.1511 (Core)git

教程歸檔在個人Github中歡迎修正和Stargithub

cnpm搭建

安裝

npm install -g --build-from-source cnpmjs.org cnpm sqlite3
# 若是報錯或者警告經過下面方式安裝
npm install -g --unsafe-perm --verbose --build-from-source cnpmjs.org cnpm sqlite3
複製代碼

若是安裝不流暢經過下面形式安裝:web

npm install -g --build-from-source \
  --registry=https://registry.npm.taobao.org \
  --disturl=https://npm.taobao.org/mirrors/node \
  cnpmjs.org cnpm sqlite3
複製代碼

若是報警告或者安裝錯誤,請添加參數--unsafe-perm --verbosesql

啓動並配置服務

管理員:myname,othername
範圍:my-company-name,other-name
默認端口:7001-registry, 7002-webnpm

啓動服務json

$ nohup cnpmjs.org start --admins='myname,othername' \
  --scopes='@my-company-name,@other-name' &
複製代碼

設置註冊地址

將cnpm默認註冊地址更改成私有註冊地址瀏覽器

cnpm set registry http://localhost:7001
複製代碼

登陸cnpm

$ cnpm login
Username: myname
Password: ***
Email: (this IS public) test@test.com
複製代碼

包上傳到私有倉庫

新建項目bash

$ cd /tmp
$ mkdir helloworld && cd helloworld
$ cnpm init
name: (helloworld) @my-company-name/helloworld
version: (1.0.0)

{
  "name": "@my-company-name/helloworld",
  "version": "1.0.0",
  "description": "my first scoped package",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
複製代碼

上傳到私有倉庫ssh

$ cnpm publish
+ @my-company-name/helloworld@1.0.0
複製代碼

查看預覽包

瀏覽器中預覽

open http://localhost:7002/@my-company-name/helloworld
複製代碼

使用cnpm預覽

cnpm info
複製代碼

安裝

全部公共包均可直接使用cnpm安裝

cnpm install hotkeys-js
複製代碼

經過verdaccio搭建

verdaccio 是一個輕量級的私有npm代理註冊。(sinopia fork)

安裝

# 使用 npm 安裝
npm install -g npm

# 使用 yarn 安裝
yarn global add verdaccio
複製代碼

啓動服務

verdaccio >> verdaccio.log 2>&1 & # 後臺啓動並寫入日誌

# Verdaccio doesn't need superuser privileges. Don't run it under root.
# warn --- config file - /root/.config/verdaccio/config.yaml
# warn --- http address - http://localhost:4873/ - verdaccio/2.3.6

verdaccio --listen 4000 --config ./config.yaml # 指定配置啓動
複製代碼

添加用戶/登陸

npm adduser --registry  http://localhost:4873
複製代碼

上傳私有包

npm publish --registry http://localhost:4873
複製代碼

本地配置註冊地址

npm config list -l # 查看默認配置
# 將默認地址 https://registry.npmjs.org/ 改爲私有地址
npm set registry http://localhost:4873
# 若是您使用HTTPS,請添加適當的CA信息
#(「null」表示從操做系統獲取CA列表)
$ npm set ca null
複製代碼

Git倉庫當私有npm

這個方法得益於,npm提供的的豐富安裝方法。經過下面方法安裝:

npm i -S git+ssh://git@git.showgold.cn:npm/hello.git

npm install -S git+ssh://git@github.com:npm/npm.git#v1.0.27
npm install -S git+ssh://git@github.com:npm/npm#semver:^5.0
npm install -S git+https://isaacs@github.com/npm/npm.git
npm install -S git://github.com/npm/npm.git#v1.0.27
複製代碼

⚠️ 上面安裝須要注意:你的工程必定是在某一個組下面創建,方便管理,在生成你的包的時候package.json中的name必定要帶上範圍

創建一個私有模塊

# 假設你創建了一個Git倉庫,先克隆下來
git clone http://git.your-inc.com/companyfe/hello-private.git

# 生成 `package.json` 配置, 注意限定 `@scope` 範圍
npm init --scope=companyfe
# 提交到倉庫
git push origin master
複製代碼

⚠️ 將獲得以下依賴,注意:

name字段必須限定範圍,通常爲 GitLab group 的名字, 例如 @companyfe, 那麼 name 爲: @companyfe/hello-private
private 設爲 true 防止將私有模塊上傳到公網上去,須要手動設置一下。

{
  "name": "@companyfe/hello-private",
  "version": "1.0.1",
  "description": "",
  "main": "index.js",
  "private":true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "kenny wang <wowohoo@qq.com> (http://wangchujiang.com)",
  "license": "ISC"
}
複製代碼

安裝使用私有模塊

跟安裝開源的模塊同樣, 使用 npm install 安裝依賴便可. 私有模塊會安裝在 @scope 的子文件夾中, 例如: node_modules/@companyfe/hello-private.

# 基礎安裝
npm i -S git+ssh://git@git.your-inc.com/companyfe/hello-private.git
# 帶版本信息的,必須經過 git 打 tag
npm i -S git+ssh://git@git.your-inc.com/companyfe/hello-private.git#v1.2.0
複製代碼

將獲得以下依賴

{
  "name": "helloworld",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "@companyfe/hello-private": "git+ssh://git@git.your-inc.com/companyfe/hello-private.git#v1.2.0"
  },
  "author": "kenny wang <wowohoo@qq.com> (http://wangchujiang.com)",
  "license": "ISC"
}
複製代碼

使用私有模塊

var hello = require('@companyfe/hello-private');
複製代碼

優劣勢

很差的地方是,使用 npm update 是沒法更新私有模塊,想更新只能從新安裝一次。好處是不用搭建服務。

參考資料

相關文章
相關標籤/搜索