npm

Verdaccio搭建npm私服

其他私有源:Nexus

# 1. 安装

npm install -g verdaccio

# 检查安装是否成功
verdaccio --version

# 2. 启动

# 2.1启动
verdaccio

# 2.2关闭
pkill -f verdaccio

# 2.3访问地址
http address - http://localhost:4873/ - verdaccio 6.x.x

# 2.4读取权限
sudo chmod -R 777 ./storage

# 2.5修改启动端口为:8080
verdaccio --listen 0.0.0.0:8080

# 2.6 配置文件路径
# 2.6.1
Linux/macOS: ~/.config/verdaccio/config.yaml

# 2.6.2;具体路径运行以后可以在终端看到
Windows: %APPDATA%\verdaccio\config.yaml
# 配置如下:
listen:
# - localhost:4873            # default value
# - http://localhost:4873     # same thing
- 你的IP地址:4873            # 这种才能使用外网、和其他电脑访问

# 3. 创建用户

npm adduser --registry http://localhost:4873

# 查看当前 npm 仓库源
npm get registry

# 设置 Verdaccio 为 npm 默认仓库;--- 不建议这么用
npm set registry http://localhost:4873

# 4. 发布 npm 包

# 登录
npm login --registry http://localhost:4873

#  验证登录状态
npm whoami --registry http://localhost:4873

#  恢复默认的 npm 注册表
npm set registry https://registry.npmjs.org/

# 5. 安装

npm install my-package-name --registry http://localhost:4873

# 6. 开机自启,Windows 上不行,在 Linux 上应该可以

npm install -g pm2

# 6.1启动 Verdaccio
pm2 start `which verdaccio` --name "verdaccio"

# 6.2 设置开机自启
pm2 startup
pm2 save

# 6.3 Windows上pm2 startup报错处理,这个不一定生效;具体使用7. Windows 上使用.bat 开机自动执行
6.3.1 pm2 save
6.3.2 pm2 resurrect

6.3.3
# 使用.bat脚本
start_pm2.bat
# 内容如下:
@echo off
pm2 resurrect

# 7. Windows 上使用.bat 开机自动执行

  • 7.1 按下 Win + R 键,输入 regedit,然后按下回车键
# 当前用户:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

# 对于所有用户:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
  • 7.2 创建 npm.bat 内容如下:
@echo off
verdaccio
  • 7.3 在注册表的右侧窗格中,右键选择“新建” > “字符串值”
"C:\Users\xxx\Desktop\npm.bat"     # xxx为用户名

# 8. Windows 上使用.bat 启动多个具体项目

# 8.1. 新建一个 start.bat 文件,内容如下:

@echo off
# 独立窗口运行,如果不希望子窗口弹出,可以添加 start /B
cd /d "D:\working\hyt-exam-cliten-html" && start "" npm run dev  // 启动开发项目1
cd /d "D:\working\hyt-exam-systemhtml" && start  /B "" npm run dev  // 启动开发项目2
cd /d "D:\tool\fastgithub_win-x64" && start  "" FastGithub.UI.exe   // 启动安装程序FastGithub
pause

# 8.2. 在注册表的右侧窗格中,右键选择“新建” > “字符串值”

"C:\Users\xxx\Desktop\start.bat"     # xxx为用户名

# 9. windows 上录屏

# 创建一个.bat文件,内容如下:
start snippingtool

# 10. PowerShell 启动报错问题

具体错误如下:

verdaccio : 无法加载文件 C:\nvm4w\nodejs\verdaccio.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.micr
osoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ verdaccio
+ ~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
Set-ExecutionPolicy RemoteSigned

# 11. npm 中给指定包指向本地仓库(而非默认 npm 官方源)

# 11.1. 按 “包名” 精准配置(仅单个包)

  1. 执行命令,给目标包绑定本地仓库地址:
# 格式:npm config set <包名>:registry <本地仓库地址>
npm config set lodash:registry http://localhost:4873
  1. 验证配置:
npm config get lodash:registry  # 输出 http://localhost:4873,说明配置生效
  1. 安装目标包:
npm install lodash  # 此时 lodash 从本地仓库拉取,其他包仍用默认源

# 11.2. 按 “scope” 批量配置(某类包,如公司组织包)

若本地仓库的包都带统一前缀(如 @company/*,scope 为 @company),可批量配置该 scope 下所有包指向本地仓库(适合团队内部包)。

  1. 执行命令,给 scope 绑定本地仓库:
# 格式:npm config set @<scope>:registry <本地仓库地址>
npm config set @company:registry http://localhost:4873
  1. 安装该 scope 下的包:
npm install @company/utils  # 自动从本地仓库拉取
npm install lodash  # 非 @company 包,仍用默认源

# 11.3. 仅安装时生效(resolutions,npm 8.3+ 支持)

  1. 强制指定包的安装源,适合所有场景:
{
  "name": "my-project",
  "dependencies": {
    "@company/utils": "^1.0.0",
    "lodash": "^4.17.0"
  },
  "resolutions": {
    // 格式:"包名": "npm:包名@版本" + "registry=<本地仓库地址>"
    "@company/utils": "npm:@company/utils@^1.0.0 registry=http://localhost:4873",
    "lodash": "npm:lodash@^4.17.0 registry=http://localhost:4873"
  }
}
  1. 安装时需配合 --legacy-peer-deps(可选,避免依赖冲突):
npm install --legacy-peer-deps

# 11.4. 发布 + 安装都生效(publishConfig,适合自己的包)

{
  "name": "my-project",
  "publishConfig": {
    "@company:registry": "http://localhost:4873", // scope 批量配置
    "lodash:registry": "http://localhost:4873" // 单个包配置
  }
}

# 11.5. 写入 .npmrc 文件(本地 / 项目级)

# 单个包指向本地仓库
lodash:registry=http://localhost:4873

# 某个 scope 下的所有包指向本地仓库
@company:registry=http://localhost:4873

# 可选:本地仓库需要认证时(如 Verdaccio 设了账号密码)
//localhost:4873/_authToken=你的认证令牌

# 12. 取消配置(恢复默认源)

# 单个包
npm config delete lodash:registry

# 某个 scope
npm config delete @company:registry

# 13. 确认包的安装源

# 查看包的安装信息(包含 registry 地址)
npm ls <包名> --json | grep registry

# 示例:查看 @company/utils 的源
npm ls @company/utils --json | grep registry
# 输出 "registry": "http://localhost:4873" 即成功
上次更新: