node.jsnpmasyncapi

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?


Solution

  • 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"