可以将 `npm` 作为依赖项安装吗?

Is it okay to install `npm` as a dependency?

我们想使用 AsyncAPI to document our RabbitMQ messaging. Therefore, we installed asyncapi/generator 作为 npm 依赖项。

如果您查看 package.json,您会发现它引用了 npmi as a dependency which in turn is referencing to global-npm。如果我们想要 运行 它,全局安装的节点和 npm 是必要的。

现在,如果我们 运行 生成器 ($ ag ./docs/asyncapi.yaml @asyncapi/html-template --output ./docs/asyncapi/ --force-write) 在没有全局安装 npm 的机器上出现以下错误消息:

/path/to/project/node_modules/global-npm/index.js:13
  throw err
  ^

Error: Cannot find module 'npm'
    at throwNotFoundError (/path/to/project/node_modules/global-npm/index.js:11:13)
    at /path/to/project/node_modules/global-npm/index.js:39:5
...

作为解决方法,我们将 npm 本身声明为依赖项:

"dependencies": {
    "@asyncapi/generator": "^1.1.4",
    "@asyncapi/html-template": "^0.15.4",
    "@asyncapi/markdown-template": "^0.11.1",
    "npm": "^6.14.9",
    ...

我从来没有见过这样的东西。这是可以接受的还是我们需要单独在我们的机器上安装我们的 npm?

如果你 运行 ag 你一定是一开始就用 npm 安装的吧?所以 npm 很可能已经在这台机器上了。

你遇到的错误,我只在 windows 上看到过,当你将生成器作为依赖项时,很可能你使用了 nvm。

解决方案已 this, so manual bump of global-npm to have thisnpmi 中修复。这是一个解决方法,从长远来看,我认为我们需要摆脱对生成器的 npmi 依赖,我认为

我们仅在 CI/CD 管道中遇到此问题,该管道使用正在安装 node/npm 的 maven-frontend-plugin。 npm 位于 node/node_modules。这不是 AsyncAPI 查找 npm 的位置。为了解决这个问题,我们 link npm-cli.js (这是 npm)来自 maven-frontend-plugin 到 AsyncApi 查找 npm node_modules/.bin.

的众所周知的地方

- ln -sf "$(pwd)/node/node_modules/npm/bin/npm-cli.js" "$(pwd)/node_modules/.bin/npm"