How I can skip installing optional dependencies from package-lock.json by npm ci?
How I can skip installing optional dependencies by 'npm ci'?
37.2k Views Asked by art201214 AtThere are 6 best solutions below
On
I was facing this issue with CI workflow script and even "--no-optional" was not working
npm ci --no-optional
The above command only worked when I added the optional package as
"optionalDependencies": {
"fsevents": "^2.3.2"
}
in the package.json file
On
It's not a proper solution, rather an ugly one, but it helped me out. It looks like npm ci --no-optional doesn't work and probably never worked. But at the same time flag --production works. And if we afford mutating package.json (e.g. in a docker container) then...
So I wrote a simple script that:
- reads
package.jsoncontent Object.assign(cfg.dependencies, cfg.devDependencies)delete cfg.devDependencies- overwrites the initial
package.json
So finally we have:
dependenciescontains both normal & dev dependenciesdevDependenciessection is emptyoptionalDependenciesare intact
And when we run npm ci --production we got what we want - no optional dependencies (in my case cypress). Due to the fact that all these steps are performed inside of a docker container we can mutate package.json.
But I'm not sure that it'll help you too.
On
You can use npm ci --no-optional . If npm still installs the optional package. Then try after removing package.lock.json and run the command again.
On
I am using npm version 9.8.0: the --no-optional flag is deprecated, you can use --omit=optional or --include=optional.
I added this to my .npmrc file (in the root folder of my project, which by the way is using npm workspaces)
omit=optional
Now, if I run npm ci (or npm install) in my project, it does not install optional dependencies by default.
To install optional dependencies pass the --include=optional flag, for instance
npm ci --include=optional
In order to make
npm ci --no-optionalskip/ignore an optional pacakge, it's important to understand hownpmintracts with package.json and pacakge-lock.json.npm install --no-optional(is only effective if pacakge-lock.json doesn't exists otherwise it would ignore --no-optional)*npm ci --no-optionalis only effective if pakcage-lock.json was already created withnpm install --no-optional**.* This means if you want to make an already installed package an optional, you can would have to
"optionalDependencies":either manulally or throughnpm install pacakge-name --save-optionalrm -rf node_modules/npm install --no-optionalnpm ci --no-optionalisn't suppose to install it.** TIP: you could debug if a certian package is assigned as optional by running
npm ls package-nameNote: This one the reason why its recommended to keep trak pacakge-lock.json with git repo of the project.