npmnpm-installnexusnexus3npm-package

How to Install All Dependency Types (Prod, Dev, Optional, Peer) of an NPM Module?


My goal is to download the entire dependency tree of a npm project, including production, development, optional, and peer dependencies, so I can upload them to my offline Sonatype-Nexus-Repository.

According to the npm documentation, the --include option can enable the installation of different types of dependencies, such as optional, development, peer, and production dependencies. For example:

npm install PACKAGE_NAME --no-save --include=prod --include=dev --include=optional --include=peer

However, neither a plain npm install nor the above command successfully downloads all dependencies. For instance, let’s consider the dependencies of vite@5.4.11 as defined in its package.json:

"dependencies": {
  "esbuild": "^0.21.3",
  "postcss": "^8.4.43",
  "rollup": "^4.20.0"
},
"optionalDependencies": {
  "fsevents": "~2.3.3"
},
"devDependencies": {
  "@ampproject/remapping": "^2.3.0",
  "@babel/parser": "^7.25.6",
  "@jridgewell/trace-mapping": "^0.3.25",
  "sass": "^1.77.8",
  "sass-embedded": "^1.77.8",
  ...
},
"peerDependencies": {
  "@types/node": "^18.0.0 || >=20.0.0",
  "less": "*",
  "lightningcss": "^1.21.0",
  ...
}

When I run the above command (npm install with --include flags), it only installs the dependencies and skips the devDependencies, optionalDependencies, and peerDependencies.

For example, after running:

npm install vite@5.4.11 --no-save --include=prod --include=dev --include=optional --include=peer

The output of the ls command in the node_modules folder looks like this:

@esbuild  esbuild  nanoid  picocolors  postcss  @rollup  rollup  source-map-js  @types  vite

This list includes only the main dependencies, while the other types are completely ignored.

What I’m looking for:

I need a way to install ALL dependencies of an npm module, including:

dependencies
devDependencies
optionalDependencies
peerDependencies

Is there a specific npm command or a workaround that ensures all dependency types are installed together?

(Additional Information): Why do I need all packages? My goal is to upload all dependencies of a project to an offline, private npm repository (Sonatype Nexus Repository). This repository will serve as the package source, enabling me to perform npm install and retrieve all required packages directly from the private repository.


Solution

  • npm install will not download its dev dependencies, because they are supposed to be installed only when developing the package. To install all the dependencies, you should run the command from within the package.

    If you need this for an arbitrary package, you can do this: get the package's package.json, put it in an empty temp directory, and run npm install there - all the dependencies will be installed.

    Here's how you can bring the package.json of a package (run it in an empty directory and it will put vite's package.json there):

    curl $(npm view vite@5.4.11 | grep tarball | awk '{print$2}') | tar -x --strip-components=1 package/package.json
    

    Now run npm install and it will install all the dependencies.

    Note: specifically for this vite example, npm install fails with:

    npm ERR! Unsupported URL Type "link:": link:./src/types
    

    To make it pass, you can edit the package.json and just remove the two link dependencies from there.