npm ENOENT no such file or directory — how to fix

# 1. Confirm that npm is running in the project root
pwd
ls -la package.json

# 2. Restore dependencies without deleting the lockfile
npm install

# In CI or when package-lock.json is committed:
npm ci
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /home/user/project/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open 'package.json'

ENOENT is the operating system error for a missing path. The value after npm ERR! path identifies what npm could not open, so inspect that path before deleting dependencies or clearing the cache.

Read the missing path before changing the install

The same ENOENT code can mean different things. npm may be looking for package.json in the wrong directory, a script may reference a file that is not checked out, or a dependency install may be incomplete.

Start with the path printed in the error and compare it with the current directory. npm prefix also shows the nearest directory npm treats as the project root.

pwd
npm prefix
ls -la
test -f package.json && echo "package.json exists" || echo "package.json is missing"

Restore only the missing project input

If the path points to package.json, move to the directory that contains the existing project file. If this is a new project, initialize it with npm init. If it is an existing project, restore the file from version control instead of creating a replacement.

If the path is inside node_modules, install from the project manifest. Keep package-lock.json when it represents the dependency versions that the project expects.

# Existing project
cd /path/to/project
npm install

# Reproducible install when package-lock.json is present
npm ci

# New project only
npm init -y

Wrong working directory

npm resolves a local package.json from the current project context. A shell opened one directory above the repository can therefore produce ENOENT even though the file exists.

# Make sure you are in the project root
cd /path/to/my-project
ls -la package.json
npm install

Missing package.json in an existing project

Do not run npm init automatically in an existing repository. Check the repository status and restore the file if it was deleted or the command ran from the wrong checkout.

git status --short
git restore package.json

# If the file belongs to another commit, inspect history first:
git log -- package.json

Path issues with npm scripts

If a script references a file that does not exist, you get ENOENT when running it.

# package.json scripts
"scripts": {
  "build": "node scripts/build.js"
}

Missing files under node_modules

When the missing path is inside node_modules, reinstall from the manifest first. Delete the dependency directory only when the install itself is known to be inconsistent, and never delete the lockfile as a routine ENOENT fix.

# Keep package-lock.json and reinstall
rm -rf node_modules
npm ci

# Check the cache before considering a cache repair
npm cache verify