npm ci is a command in the Node package manager (NPM) that stands for “NPM Clean Install”. It is used to install packages from the package-lock.json file. It is similar to npm install, but instead of reading from the package.json file, it reads from the package-lock.json file, which contains an exact record of all the packages and their dependencies, including specific versions. It uses package.json to only validate that there is no version mismatch.
The npm ci command is intended to be used in continuous integration (CI) and automated build environments, where you want to ensure that the dependencies are exactly the same every time, and there are no mismatches or conflicts between different versions of packages.
This command is faster and more reliable as compared to npm install, as it skips some of the steps like dependency resolution and network traversal, and it installs packages in a deterministic way, based on the exact version numbers specified in the lock file. Also, see how to install PNPM on Ubuntu 22.04 or 20.04.
Although the npm ci command is similar to the npm install, with some key differences:
npm ci aliases are npm clean-install, npm ic, npm install-clean, and npm isntall-clean. You can use any of these aliases to start working with this command line tool.
To use it your project folder must have package lock file and up to date install:
1. npm ci installs exact package versions
Unlike npm install, npm ci installs packages exactly as specified in the package-lock.json file. This makes it easier to ensure that all developers on a project are using the same versions of dependencies.
2. npm ci does not use the cache
npm install uses a local cache to speed up installs, but npm ci does not use this cache. Instead, it downloads packages directly from the registry. This ensures that the installed packages are exactly the same as those specified in the lock file, without any version mismatches or conflicts.
3. npm ci deletes the node_modules directory first
Before installing packages, npm ci removes the node_modules directory and installs the dependencies from scratch. This helps to ensure that there are no stale dependencies left over from previous installs.
4. npm ci is faster than npm install
Because it does not use the cache and installs exact package versions, npm ci can be faster than npm install. This is especially true in CI/CD (Continuous Integration/Continuous Deployment) pipelines, where speed and consistency are critical. Even in some cases NPM CI is twice faster than NPM I.
For detailed command line syntax of “Clean Install or CI,” you can refer to the official documentation. If you are working on large projects or those with many dependencies, you can start using npm ci as it can help to eliminate the risk of version mismatches or conflicts.