如何创建 verdaccio 的离线私有注册表

How to create offline private registry of verdaccio

我用 verdaccio 创建私有 npm registry

我希望能够 运行 npm install --registry="http://localhost:4873" 并从私有 registry.

获取所有依赖项

我需要发布项目 node_modules 目录中的所有包。

我不得不在 node_module 目录中的每个包中 运行 npm publish。(我找不到更好的方法。)

更多人发布成功但在某些情况下,我遇到了错误。例如在 zone.js 包中:

npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! zone.js@0.8.29

prepublish: `tsc && gulp build` npm ERR! Exit status 2 npm ERR! npm

ERR! Failed at the zone.js@0.8.29 prepublish script. npm ERR! This is

probably not a problem with npm. There is likely additional logging

output above. npm WARN Local package.json exists, but node_modules

missing, did you mean to install?

或在acorn包中:

acorn@5.7.3 build:main C:\Users\Admin\Desktop\test ng\ng-prj\node_modules\acorn

rollup -c rollup/config.main.js

'rollup' is not recognized as an internal or external command,

operable program or batch file.

npm ERR! code ELIFECYCLE

npm ERR! errno 1

npm ERR! acorn@5.7.3 build:main: `rollup -c rollup/config.main.js`

npm ERR! Exit status 1

npm ERR!

npm ERR! Failed at the acorn@5.7.3 build:main script.

npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm WARN Local package.json exists, but node_modules missing, did you mean to install?

有简单的方法吗?

此处为 Verdaccio 维护者。

I want to able to run npm install --registry="http://localhost:4873" and get all dependencies from private registry.

您想要的是拥有一个包含所有依赖项的离线注册表。全部发布 node_modules 不切实际,几乎不可能。

more of them published successfully But in some case, I encountered with the error. for example in zone.js

这就是重点,您需要构建每个依赖项,这没有意义。一个普通的项目很容易有成千上万的依赖和子依赖。更不用说您会失去未来依赖项更新的机会。

所以,您需要的是缓存正确存储文件夹中的所有依赖项。

  1. 运行 绿色 $> verdaccio
  2. 确保你在线
  3. 运行 npm install --registry="http://localhost:4873
  4. 安装完成后,检查您的本地缓存,请参阅 here how to find it。您应该能够在缓存中看到所有已解析的依赖项。
  5. 如果您想要真正的离线体验,请在配置文件中注释 proxy,如下所示
packages:
  '@*/*':
    access: $all
    publish: $authenticated
    # proxy: npmjs

  '**':
    access: $all
    publish: $authenticated
    # proxy: npmjs

If you comment out proxy Verdaccio won't ask for any update to the remotes, by default is npmjs, thus, no connection to external networks will be performed.

  1. 重新启动 Verdaccio
  2. 根据需要重复该过程。

所以,这里是这种方法的优点。

  1. 当你回到离线状态时(你必须再次注释掉代理部分)你将允许 Verdaccio 解析你是否有新的依赖项被缓存(如果你正在使用 semver 例如:lodash: ^1.5.6
  2. 您将拥有真正的安装体验,不用担心删除 node_modules 并清理 npm cache
  3. 存储只是一个文件夹,因此您可以将它移植到另一个地方(通过 USB 或 LAN)
  4. 与多个项目和节点包管理器工具(yarn、npm 或 pnpn)共享缓存
  5. 您不必在 node_modules 中发布每个包,因此请参阅第 2 点)。

希望对您有所帮助。此外,还有其他与离线模式相关的做法,但仅限于 yarn.

我们采纳了上面 Juan Picado 的建议。这是我们所做的:

  1. 在 /home/verdaccio/config.yaml
  2. 编辑 verdaccio 的配置文件
  3. 确保允许代理
  4. 将 npm 注册表设置为指向您的 verdaccio 实例
  5. 在系统上创建文件夹(任意文件夹)并运行 npm install 命令下载包
  6. 检查 /home/verdaccio/storage/ 目录。下载的包及其依赖项现在应该位于该目录中。
  7. 编辑 verdaccio 的配置文件,注释掉两个“代理”行以便关闭代理
  8. 重启verdaccio

此时 运行ning npm install 命令将只指向您的 verdaccio 实例而不会指向 registry.npmjs.com 并且 /home/verdaccio/storage 中的包将是您的离线可用包。