Home / Blog / Runners and limits
Why does npm ci say it "can only install with an existing package-lock.json"?
"The `npm ci` command can only install with an existing package-lock.json" means no lockfile where npm ci ran. Three causes, a git check and the fixes.
“npm ERR! The `npm ci` command can only install with an existing package-lock.json or npm-shrinkwrap.json with lockfileVersion >= 1.” means npm found no lockfile in the directory where npm ci ran. The 3 usual causes: the lockfile was never committed, the step runs in the wrong directory, or the project uses yarn or pnpm. Commit package-lock.json, or run the install where it is.
- npm ci installs exactly what the lockfile records, and found none in the directory it ran in.
- Usual causes: the lockfile is not committed, the step runs in the wrong directory, or the project uses yarn or pnpm.
- git ls-files shows which lockfiles are committed, and git check-ignore -v names the rule hiding one.
- npm 9.9.4 printed the message with npm ERR! and npm 10.9.9 with npm error.
- A lockfile that disagrees with package.json gives a different message, about being in sync.
Newer npm prints the same sentence with a different prefix: npm 9.9.4 wrote npm ERR! and npm 10.9.9 wrote npm error when I ran both, and the current source of npm words it without “or npm-shrinkwrap.json”. The step then fails with exit code 1. This post covers why npm ci insists on the file, the causes in order, how to tell which is yours, and the fixes.
What does “The npm ci command can only install with an existing package-lock.json” mean?
npm ci installs exactly what the lockfile records. npm’s documentation lists the differences from npm install: the project must have an existing package-lock.json, a lockfile that disagrees with package.json is an error instead of being updated, and the command never writes to either file. In npm’s source the message is what the command throws when it cannot load a lockfile from the directory it was run in. It cannot make one for you; that is the point of it.
What causes it?
1. The lockfile is not in the repository. It exists on your machine, so npm ci works there, and the runner’s fresh checkout does not have it. Either it was never committed or an ignore rule keeps it out: with package-lock.json in .gitignore, git add -A skipped the file without a word when I tried it. A project with package-lock=false in .npmrc never has one to commit, because npm install wrote none.
2. The step runs in the wrong directory. A run step starts at the repository root. If package.json and its lockfile live in a subdirectory, such as frontend/, the root has no lockfile and npm says so.
3. The project does not use npm. A repository with a yarn.lock or a pnpm-lock.yaml has a lockfile, but not one npm ci reads.
How do I tell which one I have?
Ask git, since the runner only has what git has. From the repository root:
git ls-files | grep -E '(package-lock\.json|npm-shrinkwrap\.json|yarn\.lock|pnpm-lock\.yaml)$'
git check-ignore -v package-lock.json
The first command lists every lockfile that is committed, with its path. No output means cause 1. A path inside a subdirectory means cause 2, and a yarn.lock or pnpm-lock.yaml with no package-lock.json means cause 3. The second command prints the ignore rule that hides the file, with the file and line it is on, and prints nothing when no rule does.
How do I fix it?
Commit the lockfile. Run npm install once, which writes package-lock.json, remove any ignore rule for it, and commit the file. In my test directory npm ci passed as soon as the file existed. The setup-node action’s documentation gives the same advice: keep the lockfile committed and use npm ci in the workflow.
Run it where the lockfile is. Set working-directory on the step, or once for the job under defaults.run. If you use setup-node’s cache, point it at the same file with cache-dependency-path: by default it looks for the lockfile in the repository root, and its own error for not finding one begins “Dependencies lock file is not found in”.
jobs:
test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: frontend
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
cache-dependency-path: frontend/package-lock.json
- run: npm ci
- run: npm test
Use the project’s own package manager. For Yarn 2 and later use yarn install --immutable (Yarn 1: --frozen-lockfile), and for pnpm pnpm install --frozen-lockfile; each refuses to change its lockfile, as npm ci does. Do not switch the step to npm install to make the error go away: npm’s documentation is clear that npm ci is the one that never writes to the lockfile, and that is what a build wants.
What about “package.json and package-lock.json are in sync”?
That is a different message with a different cause: the lockfile is there and disagrees with package.json. I got it by adding a dependency to package.json by hand, and npm listed the package missing from the lockfile underneath. The fix is to run npm install locally and commit the updated lockfile with the change.
Once the install works, the post on when a cache pays has timings for caching npm’s directory against the installed tree, the cache reference covers keys built from the lockfile, and the exit code post explains the “exit code 1” line that follows this error. The error is npm’s and depends only on the files in the checkout, so it is the same on any runner, ours included. Every size we run, with its memory, disk and rate a minute, is on our managed GitHub Actions runners page, and the troubleshooting page lists the messages a job page there can show.
Sources
- ci.js (npm/cli source) (github.com)
- npm-ci (npm documentation source) (github.com)
- Workflow syntax for GitHub Actions (docs.github.com)
- Advanced usage (actions/setup-node) (github.com)
- actions/setup-node (github.com)
- cache-restore.ts (actions/setup-node source) (github.com)
- yarn install --frozen-lockfile (Yarn 1 documentation) (classic.yarnpkg.com)
Frequently asked questions
Why does npm ci say it "can only install with an existing package-lock.json"?
npm found no lockfile in the directory where npm ci ran. The 3 usual causes are a lockfile that was never committed, a step running in the wrong directory, and a project that uses yarn or pnpm. Commit package-lock.json, or run the install where it is.
What causes it?
The lockfile is not in the repository, often because an ignore rule keeps it out. Or package.json and its lockfile are in a subdirectory and the step runs at the root. Or the project has a yarn.lock or pnpm-lock.yaml, which npm ci does not read.
How do I tell which one I have?
Run git ls-files and look for package-lock.json, yarn.lock and pnpm-lock.yaml. No lockfile means it was never committed, and git check-ignore -v package-lock.json prints the rule hiding it. A path in a subdirectory means the step runs in the wrong place.
How do I fix it?
Run npm install once and commit the package-lock.json it writes, removing any ignore rule. For a subdirectory, set working-directory and setup-node's cache-dependency-path. For pnpm, use pnpm install --frozen-lockfile; for Yarn 2 and later, yarn install --immutable (Yarn 1: --frozen-lockfile).