基于verdaccio的npm私有仓库搭建和使用总结

资源

  • 官方:https://github.com/verdaccio/...
  • 参考:https://blog.csdn.net/yyzzhc9...
  • 参考:https://www.jianshu.com/p/16b...
  • 参考:https://www.jianshu.com/p/1d0...

原来用sinopia搭建的私服,但sinopia两年年停止维护了,现在改为verdaccio。使用verdaccio也超级简单,只需几分钟就可以搭建一个私服,适合公司内部不对外的包的安装

一:安装和配置

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 "[email protected]" with binaries:
      - verdaccio
Done in 52.10s.

安装到了 C:\Users\Administrator\AppData\Local\Yarn\bin[我的是win10]

npm install --global verdaccio安装

B.测试

随便建一个空目录测试,我当前工作目录是F:\youshengyouse\del3

$ 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/,结果如下

二:添加用户

另开一个命令行窗口,原来的verdaccio仍在运行

$ npm adduser --registry  http://localhost:4873
Username: abcd
Password: qwer
Email: (this IS public) [email protected]
Logged in as abcd on http://localhost:4873/.

三:发布私包

当前工作目录:要发布哪个包,当前目录切换到包根目录下

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

> [email protected] prepare .
> cross-env NODE_ENV=production npm run build


> [email protected] 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: [email protected]
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
+ [email protected]

现在刷新http://localhost:4873/#/就可以看到刚才发布的包
问题:本地安装的包放在哪里了呢?

四:安装包

npm set registry http://localhost:4873/

查看仓库

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
+ [email protected]
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

你可能感兴趣的:(yarn,npm,verdaccio)