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
 - 0.0.0.0: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. 清理 Windows 上的 vscode 缓存文件

# clear_vscode_cach.bat

@echo off
chcp 65001 >nul
echo ==============================================
echo VSCode workspaceStorage 项目缓存清理工具
echo 提示:请【先完全关闭所有VSCode窗口】再执行!
echo ==============================================


:: 拼接缓存目录路径
set "cacheDir=%APPDATA%\Code\User\workspaceStorage"

if not exist "%cacheDir%" (
    echo 目录不存在:%cacheDir%
    echo 无需清理,按任意键退出
    pause >nul
    exit /b
)

echo.
echo 正在删除目录内所有缓存文件夹...
rd /s /q "%cacheDir%"
:: 重建空文件夹(防止vscode报错找不到路径)
md "%cacheDir%"

echo.
echo ✅ 清理完成!
echo 注意:下次打开TS项目,首次启动会重新构建类型索引
echo ==============================================

# 8.2. 清除用户临时文件

@echo off
chcp 65001 >nul
echo ==============================================
echo          清理当前用户临时文件 %TEMP%
echo ==============================================
echo.
echo 路径:%TEMP%
echo 提示:正在使用中的文件会自动跳过,无需关闭所有软件
echo.


del /f /s /q "%TEMP%\*"
rd /s /q "%TEMP%\*" 2>nul

echo.
echo ✅ 用户临时文件清理执行完毕!

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

@echo off
# 解决cmd中乱码
chcp 65001 > nul
# 独立窗口运行,如果不希望子窗口弹出,可以添加 start /B
call "C:\Users\Administrator\Desktop\bat\clear_vscode_cach.bat"
echo ======================vscode缓存清理完成,启动项目服务======================

# 启动开发项目1
cd /d "E:\works\hyt-exam-cliten-html" && start /min  "" npm run dev

# 启动开发项目2
cd /d "E:\works\hyt-exam-invigilator-html" && start  /b "" npm run dev
pause

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

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

# 8.5. 最小化启动'cmd'和'exe'

@echo off
:: 如果没有标记,重新启动自身并最小化,原窗口直接退出
if not "%1"=="min" (start /min cmd /c "%~f0" min & exit)

:: ==========下面是你的业务代码==========
start /B verdaccio

cd /d "E:\installFiles\fastgithub_win-x64"
start /min FastGithub.UI.exe

:: 执行完成,cmd自动关闭

# 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" 即成功
上次更新: