基於verdaccio的npm私有倉庫搭建和使用總結

資源node

原來用sinopia搭建的私服,但sinopia兩年年中止維護了,如今改成verdaccio。使用verdaccio也超級簡單,只需幾分鐘就能夠搭建一個私服,適合公司內部不對外的包的安裝git

一:安裝和配置

A.安裝

$ yarn global add verdaccio
yarn global v1.12.3
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "verdaccio@3.10.1" with binaries:
      - verdaccio
Done in 52.10s.

安裝到了 C:\Users\Administrator\AppData\Local\Yarn\bin[個人是win10]github

npm install --global verdaccio安裝shell

B.測試

隨便建一個空目錄測試,我當前工做目錄是F:\youshengyouse\del3npm

$ verdaccio
 warn --- config file  - C:\Users\Administrator\.config\verdaccio\config.yaml
 warn --- Plugin successfully loaded: htpasswd
 warn --- Plugin successfully loaded: audit
 warn --- http address - http://localhost:4873/ - verdaccio/3.10.1

在瀏覽器輸入http://localhost:4873/,結果以下
圖片描述json

二:添加用戶

另開一個命令行窗口,原來的verdaccio仍在運行瀏覽器

$ npm adduser --registry  http://localhost:4873
Username: abcd
Password: qwer
Email: (this IS public) a@163.com
Logged in as abcd on http://localhost:4873/.

三:發佈私包

當前工做目錄:要發佈哪一個包,當前目錄切換到包根目錄下緩存

$ npm publish --registry http://localhost:4873

> gatsby2-cli@1.0.0 prepare .
> cross-env NODE_ENV=production npm run build


> gatsby2-cli@1.0.0 build F:\youshengyouse\frontend\packages\gatsby\packages\gatsby2-cli
> babel src --out-dir lib --ignore **/__tests__

Successfully compiled 6 files with Babel.
npm notice
npm notice package: gatsby2-cli@1.0.0
npm notice === Tarball Contents ===
npm notice 1.8kB package.json
npm notice 7.4kB CHANGELOG.md
npm notice 3.3kB README.md
npm notice 9.4kB lib/create-cli.js
npm notice 2.7kB lib/index.js
npm notice 4.7kB lib/init-starter.js
npm notice 2.0kB lib/reporter/errors.js
npm notice 3.1kB lib/reporter/index.js
npm notice 4.0kB lib/reporter/prepare-stack-trace.js
npm notice === Tarball Details ===
npm notice name:          gatsby2-cli
npm notice version:       1.0.0
npm notice package size:  11.0 kB
npm notice unpacked size: 38.3 kB
npm notice shasum:        64c9c47b81610731e559bc33f86aa02f87155656
npm notice integrity:     sha512-vhroNpnWCwivE[...]8hAg+z6SPOeyw==
npm notice total files:   9
npm notice
+ gatsby2-cli@1.0.0

如今刷新http://localhost:4873/#/就能夠看到剛纔發佈的包
問題:本地安裝的包放在哪裏了呢?babel

四:安裝包

npm set registry http://localhost:4873/

查看倉庫frontend

npm config get registry
$ npm init # 若是沒有package.json文件得先建
$ yarn add gatsby2-cli

發現gatsby2-cli是在本地的,其他的依賴仍是從npmjs.com倉庫中安裝的

問題

問題1:太費空間,想辦法解決,全局緩存在C盤
解決辦法:從新配置npm
npm的配置文件是 C:\Users\Administrator\.npmrc

看下配置

$ npm config list
; cli configs
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.4.1 node/v10.15.0 win32 x64"

; builtin config undefined
prefix = "C:\\Users\\Administrator\\AppData\\Roaming\\npm"

; node bin location = E:\Program Files\nodejs\node.exe
; cwd = F:\youshengyouse\del3
; HOME = C:\Users\Administrator
; "npm config ls -l" to show all defaults.

修改prefix爲 E:\Program Files\nodejs

# 修改緩存地址
$ npm config set cache "E:\Program Files\nodejs\node_cache2019"
# 修改全局放置位置
$ npm config set prefix "E:\Program Files\nodejs"

如今配置以下

$ npm config list
; cli configs
metrics-registry = "http://localhost:4873/"
scope = ""
user-agent = "npm/6.4.1 node/v10.15.0 win32 x64"

; userconfig C:\Users\Administrator\.npmrc
ca = null
cache = "E:\\Program Files\\nodejs\\node_cache2019"
prefix = "E:\\Program Files\\nodejs"
registry = "http://localhost:4873/"

; builtin config undefined

; node bin location = E:\Program Files\nodejs\node.exe
; cwd = F:\youshengyouse\frontend\packages\gatsby\www
; HOME = C:\Users\Administrator
; "npm config ls -l" to show all defaults.

全局安裝

$ npm i -g gatsby-cli
E:\Program Files\nodejs\gatsby -> E:\Program Files\nodejs\node_modules\gatsby-cli\lib\index.js
+ gatsby-cli@3.0.0
added 211 packages from 119 contributors in 121.998s

會安裝在E:\Program Files\nodejs\node_modules\gatsby-cli,同時E:\Program Files\nodejs\gatsby和gatsby.cmd兩個shell命令也同時安裝
配置環境變量 E:\Program Files\nodejs\node_modules,這樣在任何地方能夠全局執行gatsby

恢復官方倉庫

npm config set registry https://registry.npmjs.org
相關文章
相關標籤/搜索