Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can skip installing optional dependencies by 'npm ci'?

How I can skip installing optional dependencies from package-lock.json by npm ci?

like image 635
art201214 Avatar asked Dec 29 '18 12:12

art201214


People also ask

How do I ignore optional dependencies?

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.

How do I skip a package in npm install?

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.

Why use npm ci instead of npm install?

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.

Does npm install install optional dependencies?

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).


4 Answers

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.

like image 57
Anshu Kumar Avatar answered Oct 13 '22 12:10

Anshu Kumar


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.

like image 32
Maxime Gélinas Avatar answered Oct 13 '22 10:10

Maxime Gélinas


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

like image 2
Syed Zain Jeelani Avatar answered Oct 13 '22 11:10

Syed Zain Jeelani


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.json content
  • Object.assign(cfg.dependencies, cfg.devDependencies)
  • delete cfg.devDependencies
  • overwrites the initial package.json

So finally we have:

  • dependencies contains both normal & dev dependencies
  • devDependencies section is empty
  • optionalDependencies are 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.

like image 1
faiwer Avatar answered Oct 13 '22 11:10

faiwer