Skip to content

/errors/npm/enoent-could-not-read-package-json

npm error code ENOENT: Could not read package.json

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · 11.19.0updated
tool
npm
versions
npm 7 – 11
os
any

npm error code ENOENT ... Could not read package.json: Error: ENOENT: no such file or directory means npm looked for package.json in the current directory and found nothing. ENOENT is the operating system's "no such file or directory" code. npm does not search parent folders for scripts and project commands, so run it from the project root (the folder that contains package.json), or create the file with npm init -y.

Reproduce it

bash
npm run build 2>&1 | sed -E 's/[0-9T_:.-]+Z-debug-0\.log/<timestamp>-debug-0.log/'
echo "exit status: ${PIPESTATUS[0]}"

Output

npm error code ENOENT
npm error syscall open
npm error path /tmp/work/package.json
npm error errno -4058
npm error enoent Could not read package.json: Error: ENOENT: no such file or directory, open '/tmp/work/package.json'
npm error enoent This is related to npm not being able to find a file.
npm error enoent
npm error A complete log of this run can be found in: /tmp/work/.npm-cache/_logs/<timestamp>-debug-0.log
exit status: 127

The path in the message (/tmp/work/package.json here) is where npm looked; compare it with where your package.json is. The errno value and the exit status are platform-specific (this run was on Windows, where ENOENT is errno -4058); scripts should only test for a non-zero status.

Fix

bash
npm init -y >/dev/null
npm pkg set scripts.build="echo building"
npm run build

Output

> work@1.0.0 build
> echo building

building

In CI and Docker

Dockerfile
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
.github/workflows/ci.yml
- run: npm ci
  working-directory: frontend

Sources