Joe
Joe

Reputation: 4928

npm install is not installing dependencies of dependencies

When I npm install my node application, my the packages list in the dependencies property of my package.json are installed. But, for some reason some of those dependencies are not installing their sub-dependencies. In other words, there is no node_modules folder with the dependencies of my dependencies.

myproject
- node_modules
  - my-package
    - node_modules (would expect this to be here, but it's not)

The strange thing is that is another project, the sub-dependencies are being installed for the same packages.

Even when I try to manually install a single package via npm install my-package, that packages node_modules are not installed.

Is there a reason why this might be the case? Or a way I can debug this?

Thanks

Upvotes: 6

Views: 7773

Answers (2)

AndreC23
AndreC23

Reputation: 158

I opened a similar question and was brought back to this one where I see a lack of an actual solution. I've currently managed to find it and the correct one to get your package to install its own node_modules is to add the following to your package.json:

"bundledDependencies": [
    "npm-package"
]

To that array, add the packages you want to be installed in the node_modules folder of my-package:

myproject
- node_modules
  - my-package
    - node_modules <-- This folder will contain the packages of the array

So, an example of package json could be:

{
  "name": "my-package",
  "version": "1.0.0",
  "dependencies": {
    "cheerio": "^1.0.0-rc.10",
    "jsdom": "^19.0.0",
    "yargs": "13.2"
  },
  "bundledDependencies": [
    "yargs",
    "jsdom"
  ]
}

When you will install my-package inside one other project it will have it's own node_modules with the packages specified in bundledDependencies.

Upvotes: 2

Mike
Mike

Reputation: 617

NPM tries to flatten your dependencies at the root level. If is a version that satisfies all of your dependencies(Either only one package the dependency, or the version satisfies all package requirements as defined in package.json) it will roll it up to the root of your node_modules. This is done intentionally, so that you do not installed the same dependency multiple times.

The exception to this rule occurs if there are conflicting versions of a module e.g. package1 has dependency a version 1.0.1 and package2 has dependency a version 2.

Upvotes: 7

Related Questions