如何将项目的 package.json 设置为能够使用 verdaccio 从本地注册表安装

How to set a project's package.json to be able to install from local registry with verdaccio

我正在通过简单的项目学习使用 docker 和 kubernetes 的微服务,现在我正在尝试使用安装为带 helm 的 docker 容器的本地注册表。我在我的本地注册表中发布了我的 package/library(我正在使用 verdaccio)并使用命令“npm install @mycompany/mylibs --registry=http://localhost:4873”成功地将它安装到我当前的项目中。我的问题是当我试图通过 skaffold 将我的项目部署到 kubernetes 时,它无法从 package.json 配置文件下载包。我尝试将 .npmrc 文件设置到项目的根文件夹和 verdaccio conf 文件上的默认注册表,但都失败了。有没有人遇到和我一样的问题以及如何解决它。请有人帮忙。谢谢

这是我的项目结构:

MyProject
|-auth (this service has dependency to @mycompany/mylibs)
| |-src
| |-Dockerfile
| |-package.json
|
|-infra/kube
| |-auth-depl.yaml
| |-ingress-srv.yaml


MySharedLibrary
| |-src
| |-package.json

auth 的 package.json :

{
  "name": "auth",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "ts-node-dev src/index.ts",
    "test": "jest --watchAll --no-cache"
  },
  "jest": {
    "preset": "ts-jest",
    "testEnvironment": "node",
    "setupFilesAfterEnv": [
      "./src/test/setup.ts"
    ]
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@mycompany/mylibs": "^1.0.3",
    "@types/cookie-session": "^2.0.42",
    "@types/express": "^4.17.9",
    "@types/jsonwebtoken": "^8.5.0",
    "cookie-session": "^1.4.0",
    "express": "^4.17.1",
    "express-async-errors": "^3.1.1",
    "express-validator": "^6.7.0",
    "jsonwebtoken": "^8.5.1",
    "mariadb": "^2.5.1",
    "mysql2": "^2.2.5",
    "sequelize": "^6.3.5",
    "ts-node-dev": "^1.0.0",
    "typescript": "^4.1.2"
  },
  "devDependencies": {
    "@types/jest": "^26.0.19",
    "@types/supertest": "^2.0.10",
    "jest": "^26.6.3",
    "supertest": "^6.0.1",
    "ts-jest": "^26.4.4"
  }
}

我正在使用:

Windows 10 台 PC 作为 docker 的主要主机以及托管项目的地方。

docker 版本 19.03.13

npm 版本 6.14.6

verdaccio 4.12.0

头盔 3.5.3

skaffold 1.13.0

我使用 skaffold dev 部署项目后的错误消息:

npm ERR! 404 Not Found - GET https://registry.npmjs.org/@mycompany%2fmylibs - Not found
npm ERR! 404
npm ERR! 404  '@mycompany/mylibs@^1.0.3' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 It was specified as a dependency of 'app'
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

我希望我的部署过程中每个容器都能在本地注册表 (http://localhost:4873) 上找到所有必需的依赖项,当找不到它们时,它应该尝试访问 public npm ( https://registry.npmjs.org 或 npmjs.com).

您需要将范围与您的注册表相关联:

npm login --registry=http://localhost:4873 --scope=@mycompany

您可以找到文档 here

在此之后 npm install 如果包在 @mycompany

范围内,则从您的存储库安装包

感谢大家帮助我,我通过添加以下行在 auth 服务上重新配置我的 Dockerfile 解决了这个问题:

RUN npm config set registry http://<the_local_registry_container_IP>:4873

我成功构建了容器,但它引发了新问题“请求已被弃用,请参阅 github.com/request/request/issues/3142”,因为我正在使用超级测试。我不知道为什么我还在寻找主要问题。在另一种情况下,如果我使用 public npm repo 构建它,它不会引发该问题。