npm 私有库搭建- verdaccio (一)

本文详细介绍了如何在Linux环境下安装和配置Verdaccio私有npm仓库,包括解决权限问题、配置config.yaml文件、用户认证、发布及使用私有包的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装

npm install -g verdaccio --unsafe-perm

加上–unsafe-perm的原因

会导致某些权限上的问题,哪怕是用root去执行也是存在权限问题
错误信息如下

gyp WARN EACCES user "root" does not have permission to access the dev dir "/root/.node-gyp/10.13.0"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/src/node-v10.13.0-linux-x64/lib/node_modules/verdaccio/node_modules/dtrace-provider/.node-gyp"
gyp WARN install got an error, rolling back install
gyp WARN install got an error, rolling back install
gyp ERR! configure error 
gyp ERR! stack Error: EACCES: permission denied, mkdir '/usr/src/node-v10.13.0-linux-x64/lib/node_modules/verdaccio/node_modules/dtrace-provider/.node-gyp'
gyp ERR! System Linux 4.15.0-29deepin-generic
gyp ERR! command "/usr/src/node-v10.13.0-linux-x64/bin/node" "/usr/src/node-v10.13.0-linux-x64/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /usr/src/node-v10.13.0-linux-x64/lib/node_modules/verdaccio/node_modules/dtrace-provider
gyp ERR! node -v v10.13.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok 

修改配置(linux环境下)

默认的路径是 ~/.config/verdaccio下的config.yaml

~代表的是用户目录
不知道配置路径可以直接启动,启动时会有提示信息

zengwe@zengwe-PC:~$verdaccio
 warn --- config file  - /home/zengwe/.config/verdaccio/config.yaml
 warn --- Plugin successfully loaded: htpasswd
 warn --- Plugin successfully loaded: audit
 warn --- http address - http://localhost:4873/ - verdaccio/3.8.6

修改config.yaml

config.yaml文件内容


# This is the default config file. It allows all users to do anything,
# so don't use it on production systems.
#
# Look here for more config file examples:
# https://github.com/verdaccio/verdaccio/tree/master/conf
#

# path to a directory with all packages
storage: /home/zengwe/.local/share/verdaccio/storage
# path to a directory with plugins to include
plugins: ./plugins

web:
  # WebUI is enabled as default, if you want disable it, just uncomment this line
  #enable: false
  title: Verdaccio

auth:
  htpasswd:
    file: ./htpasswd
    # Maximum amount of users allowed to register, defaults to "+inf".
    # You can set this to -1 to disable registration.
    #max_users: 1000

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: https://registry.npmjs.org/

packages:
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated
    proxy: npmjs

  '**':
    # allow all users (including non-authenticated users) to read and
    # publish all packages
    #
    # you can specify usernames/groupnames (depending on your auth plugin)
    # and three keywords: "$all", "$anonymous", "$authenticated"
    access: $all

    # allow all known users to publish packages
    # (anyone can register by default, remember?)
    publish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: npmjs

# To use `npm audit` uncomment the following section
middlewares:
  audit:
    enabled: true

# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: verdaccio.log, level: info}
# add by zengwe
listen: 0.0.0.0:9001

这次的博客只写基础应用,并未涉及权限问题
现在启动,启动命令为verdaccio即可,就可以通过http://IP:9001访问了,linux下记得开防火墙

启动

zengwe@zengwe-PC:~$verdaccio
 warn --- config file  - /home/zengwe/.config/verdaccio/config.yaml
 warn --- Plugin successfully loaded: htpasswd
 warn --- Plugin successfully loaded: audit
 warn --- http address - http://0.0.0.0:9001/ - verdaccio/3.8.6

在这里插入图片描述

配置访问用户

生成md5的htpasswd密码,有密码生成的网站
会生成一下格式的用户名和密码

username:$apr1$809o3o2I$.pN83j6srvreYZA4NL8GF0

在~/.config/verdaccio下创建文件htpasswd
写入上面字符串,每个用户占一行
现在就可以通过上面的用户名和密码访问了

发布包

为这个私有源设置用户名和密码

zengwe@zengwe-PC:test-publish$npm adduser --registry  http://127.0.0.1:9001
Username: test
Password: 
Email: (this IS public) ××××@qq.com
Logged in as test on http://127.0.0.1:9001/.

我的在本地设置所以IP的127.0.0.1,要为源服务器的地址

新建一个项目然后发布

zengwe@zengwe-PC:mkdir test-publish && cd test-publish
zengwe@zengwe-PC:test-publish$npm init
...
zengwe@zengwe-PC:test-publish$vim index.js
...
zengwe@zengwe-PC:test-publish$vim readme.md
...

正式发布

zengwe@zengwe-PC:test-publish$npm publish --registry=http://127.0.0.1:9001
npm notice 
npm notice ?  test-publish@1.0.0
npm notice === Tarball Contents === 
npm notice 262B package.json
npm notice 30B  index.js    
npm notice 48B  readme.md   
npm notice === Tarball Details === 
npm notice name:          test-publish                            
npm notice version:       1.0.0                                   
npm notice package size:  376 B                                   
npm notice unpacked size: 340 B                                   
npm notice shasum:        a7f2f349ab226d36f3ce01572e743a2c8bb23100
npm notice integrity:     sha512-aiHJQuA1b2mJw[...]+GYdjPzM8qWcw==
npm notice total files:   3                                       
npm notice 

每次修改完在发布时修改package.json中version

使用私有npm的包

npm install test-publish --registry=http://127.0.0.1:9001

更新

npm uodate test-publish --registry=http://127.0.0.1:9001
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值