npm ERR! code ENOENT: “no such file or directory” explained and fixed
ENOENT is the operating system’s code for “Error: NO ENTry”: something tried to open a file or run a program at a path where nothing exists. npm just passes it on, and the path in the message tells you exactly what was missing.
Read the message first
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /home/sam/Downloads/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/home/sam/Downloads/package.json'
syscall says what npm was doing (open a file, spawn a program, rename, scandir a folder), and path says where. The errno -2 is just ENOENT’s number on Linux and macOS (Windows shows -4058).
The common cases
| Message | Usually means | Fix |
|---|---|---|
open '…/package.json' | You ran npm outside the project folder (often your home folder or Downloads) | cd to the folder with package.json; ls (or dir) should list it |
Could not read package.json right after cloning | The repo keeps the app in a subfolder such as frontend/ or web/ | cd frontend && npm install, or npm install --prefix frontend |
spawn git ENOENT | A dependency is installed from a git URL and git isn’t installed | Install git, open a new terminal, retry |
spawn node ENOENT / spawn sh ENOENT | A script runs with a PATH that doesn’t include node or a shell (cron, systemd, an IDE, a slim Docker image) | Use full paths, set PATH in the service, or pick an image that has them |
scandir or rename inside node_modules | An interrupted or half-finished install | Delete node_modules, then npm ci (or npm install) |
lstat …\AppData\Roaming\npm (Windows) | npm’s global folder hasn’t been created yet | Create %APPDATA%\npm, or reinstall Node |
A clean reinstall
rm -rf node_modules # Windows PowerShell: Remove-Item -Recurse -Force node_modules
npm cache verify
npm ci # exact install from package-lock.json
# no lockfile yet? use: npm install
npm ci refuses to run if package.json and package-lock.json disagree; that’s a different error, and npm install fixes it by updating the lockfile. A lockfile left full of <<<<<<< lines after a merge needs conflict resolution first; regenerate it rather than hand-editing.
ENOENT in npm scripts
If the error comes from your own "scripts" (for example npm run build failing with ENOENT: no such file or directory, open 'dist/config.json'), npm is fine; your script is reading a path relative to the wrong folder. Scripts run with the working directory set to the folder containing package.json. In Node code, build paths from the file’s own location instead of the current directory: path.join(__dirname, 'config.json'), or in ES modules new URL('./config.json', import.meta.url).
In Docker
COPY package*.json ./ copies from the build context, the folder you pass to docker build. Building from the repo root with the app in a subfolder, or a .dockerignore that excludes package.json, leads to ENOENT on the RUN npm ci step. Check with docker build --progress=plain . and make sure WORKDIR matches where you copied the files.
Is package.json itself OK?
If package.json exists but npm says Unexpected token or EJSONPARSE, the file is there but isn’t valid JSON. Paste it into the JSON formatter to find the exact line, and see Unexpected token in JSON. Fighting peer dependency errors instead? See npm ERESOLVE. Global install failing with EACCES instead? See npm EACCES permission denied on global install. OpenSSL / webpack failing with digital envelope routines unsupported? See Node OpenSSL envelope error.
Related
npm ERR! code ELIFECYCLE · Module not found: Can't resolve · Cannot find module (Node)
Questions
What does npm ERR! code ENOENT mean?
ENOENT means no such file or directory. npm tried to open a file or run a program at the path shown in the error, and nothing was there. Most often it is package.json, because npm was run outside the project folder.
How do I fix Could not read package.json?
Change into the folder that actually contains package.json before running npm, or create one with npm init -y if you are starting a new project.
What does spawn git ENOENT mean?
npm needed to run git, usually to fetch a dependency from a git URL, and could not find git on the PATH. Install git and open a new terminal.
Should I delete node_modules to fix ENOENT?
If the missing path is inside node_modules, yes: delete the folder and run npm ci or npm install to rebuild it. It won't help if the missing file is package.json.
Published .