npm install: Usage, Options & Common CI Errors
Install dependencies from package.json - and survive it in CI.
npm install reads package.json, resolves the dependency tree, writes node_modules, and updates package-lock.json. In CI it is the slow, flaky step - here is what it does and how to keep it green.
What it does
Resolves every dependency (and peer/optional deps) into a tree, fetches tarballs from the registry, writes node_modules, and creates or updates package-lock.json. Runs lifecycle scripts (preinstall, install, postinstall) unless --ignore-scripts is set.
Common usage
npm install # install all deps from package.json
npm install lodash # add a runtime dependency
npm install -D typescript # add a dev dependency
npm install --ignore-scripts # skip pre/post install scripts
npm install --no-save react # install without writing package.jsonCommon CI error: ERESOLVE peer conflict
CI fails with "npm error code ERESOLVE / could not resolve dependency tree" because a package demands a peer version another package will not satisfy. Reproduce locally, then pin the offending versions in package.json. As a last resort use --legacy-peer-deps to fall back to npm 6 resolution; prefer --legacy-peer-deps over --force, which can install a broken tree.
# fix the real conflict, or unblock CI explicitly:
npm install --legacy-peer-depsKey options
| Flag | Effect |
|---|---|
| -D, --save-dev | Add to devDependencies |
| --no-save | Do not modify package.json |
| --ignore-scripts | Skip lifecycle scripts |
| --legacy-peer-deps | Ignore peer dep conflicts (npm 6 behavior) |
| --omit=dev | Skip devDependencies |