How I can skip installing optional dependencies from package-lock.json
by npm ci
?
The --no-optional argument will prevent optional dependencies from being installed. The --no-shrinkwrap argument, which will ignore an available shrinkwrap file and use the package. json instead.
To skip Installation of devDepenencies pass --production flag to npm install ,with the --production flag(or NODE_ENV environment variable set to production ) npm will not install modules listed in devDependencies." To make any module to be part of devDependencies pass --dev while installing.
Use npm install to install new dependencies , or to update existing dependencies (e.g. going from version 1 to version 2). Use npm ci when running in continuous integration, or if you want to install dependencies without modifying the package-lock.
The installed package will be put into optionalDependencies . When you want to avoid installing optional dependencies, you can execute npm ci --no-optional (e.g. on CI tools like GitLab CI). Use npm ci to install all dependencies, including optional dependencies (e.g. on your development environment).
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.
There was an error in NPM's implementation of npm ci --no-optional
. It has been fixed in versions > 6.13.3 - maybe earlier versions as well, but I can only vouch for 6.13.4 and up.
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
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:
package.json
contentObject.assign(cfg.dependencies, cfg.devDependencies)
delete cfg.devDependencies
package.json
So finally we have:
dependencies
contains both normal & dev dependenciesdevDependencies
section is emptyoptionalDependencies
are intactAnd 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With