简体   繁体   中英

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

We want to use AsyncAPI to document our RabbitMQ messaging. Therefore, we installed asyncapi/generator as a npm dependency.

If you have a look at the package.json you can see that it references npmi as a dependency which in turn is referencing to global-npm . If we want to run it, a globally installed node and npm is necessary.

Now if we run the generator ( $ ag./docs/asyncapi.yaml @asyncapi/html-template --output./docs/asyncapi/ --force-write ) on a machine which has no globally installed npm following error message appears:

/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
...

As a workaround we declared npm itself as a dependency:

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

I've never seen such a thing. Is this acceptable or do we need to install our npm on our machines separatly?

if you run ag you must have installed it with npm initially right? so npm is most probably on this machine already.

The error you have, I saw it on windows only, when you have the generator as dependency, and most likely you use nvm.

Solution was this , so manual bump of global-npm to have this fixed in npmi . This is a workaround,long term I think we need to get rid of npmi dependency from the generator I think

We only had this issue in our CI/CD pipeline working with maven-frontend-plugin which is installing node/npm. npm is located in node/node_modules . This is no location where AsyncAPI looks for npm. In order to fix this issue we link the npm-cli.js (which is npm) from the maven-frontend-plugin to a well known place where AsyncApi looks up for npm node_modules/.bin .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM