Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of making "npm ci" install devDependencies, or "npm install" not update package-lock.json?

I'm trying to put together documentation for new developers installing our codebase on their local development environments. I'd like to give them command(s) that:

  • Installs both devDependencies and dependencies based on the versions in package-lock.json
  • Doesn't update package-lock.json

"npm ci" does almost exactly what I want, but doesn't seem to install devDependencies. "npm install" does install devDependencies, but it sometimes modifies package-lock.json.

I could imagine something janky like "npm install && git checkout package-lock.json", but I feel like there must be a more idiomatic way of saying "give me a clean install of this project's dependencies for development?"

like image 671
josh Avatar asked Feb 04 '20 21:02

josh


People also ask

Does npm ci install devDependencies?

You're correct. npm ci also installs dev dependencies. Adding --only=prod or --production would not install devDependencies and just install dependencies .

Does npm ci need package json?

The main differences between using npm install and npm ci are: The project must have an existing package-lock. json or npm-shrinkwrap.

Does npm ci update package-lock json?

Unlike npm install , npm ci will never modify your package-lock. json . It does however expect a package-lock. json file in your project — if you do not have this file, npm ci will not work and you have to use npm install instead.

Does npm install use package-lock json?

The package-lock. json file stores the version information of each installed package unchanged, and npm will use those package versions when running the npm install command.

Why does NPM CI fail to update the package lock?

If dependencies in the package lock do not match those in package.json, npm ci will exit with an error, instead of updating the package lock. npm ci can only install entire projects at a time: individual dependencies cannot be added with this command.

What is the difference between NPM install and NPM CI?

There is a package-lock.json or npm-shrinkwrap.json file. The node_modules folder is missing or empty. In short, the main differences between using npm install and npm ci are: The project must have an existing package-lock.json or npm-shrinkwrap.json.

Why is NPM CI not writing to node_modules?

If a node_modules is already present, it will be automatically removed before npm ci begins its install. It will never write to package.json or any of the package-locks: installs are essentially frozen. Make sure you have a package-lock and an up-to-date install:

Why is my NPM install not installing the correct version?

If you use ^ or ~ when you specify the version of your dependency, npm may not install the exact version you specified. npm install can update your package-lock.json when there are changes such as when you install a new dependency. It will delete your node_modules folder to ensure a clean state.


Video Answer


2 Answers

npm ci does install both dependecies and dev dependencies. But if you use npm ci --production or if your NODE_ENV is set to production, then it avoids installing dev dependencies. Please check docs here.

With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies.

NOTE: The --production flag has no particular meaning when adding a dependency to a project.

like image 147
Sai Tej Avatar answered Nov 06 '22 21:11

Sai Tej


Override NODE_ENV variable

When your NODE_ENV environment variable is set to production, using npm ci will not install devDependencies. But if you still want to install devDependencies

npm ci --include=dev

will do the trick ;)


For versions older than NPM v7.x, use

npm ci --also=dev
like image 22
Niket Pathak Avatar answered Nov 06 '22 22:11

Niket Pathak