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:
"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?"
You're correct. npm ci also installs dev dependencies. Adding --only=prod or --production would not install devDependencies and just install dependencies .
The main differences between using npm install and npm ci are: The project must have an existing package-lock. json or npm-shrinkwrap.
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.
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.
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.
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.
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:
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.
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 theNODE_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.
NODE_ENV
variableWhen 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 ;)
npm ci --also=dev
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