ENOENT (errno 2)

errno 2

LINUX

No such file or directory

Also searched as: No such file or directory, errno 2, ENOENT: no such file or directory, npm ERR! code ENOENT, FileNotFoundError

Linux terminal running strace with the failing system call and errno highlighted
Linux errno: trace the exact failing system call before changing anything.

What it means

ENOENT, 'No such file or directory' (errno 2), means a path used by the program does not exist. It is one of the most common errors in shells, Node.js ('ENOENT: no such file or directory, open ...'), Python (FileNotFoundError) and build tools. Sometimes the missing piece is not the file you expect but an interpreter or library it needs.

Applies to: Linux, macOS (BSD errno), WSL, Docker containers

How to fix ENOENT (errno 2)

  1. Print the exact path from the error and check it with 'ls -la <path>'; watch for typos and letter case, since Linux paths are case-sensitive
  2. Check the current working directory with 'pwd'; relative paths resolve from where the program was started, not from the script's folder
  3. For 'npm ERR! enoent', run the command in the folder that contains package.json, then 'npm install' if node_modules is missing
  4. If a script exists but running it gives 'No such file or directory', check its shebang interpreter exists and fix Windows line endings with 'dos2unix script.sh'
  5. Check for broken symlinks with 'ls -l' and 'readlink -f <path>'
  6. Trace which path is missing with 'strace -f -e trace=openat,execve <command> 2>&1 | grep ENOENT'
  7. In Docker, make sure the file was copied into the image or mounted, and that paths match the container, not the host

Why it happens

Any path lookup that fails on a missing component returns ENOENT: deleted or never-created files, wrong working directory, broken symlinks, missing interpreters in a shebang, or files that exist only on the host and not in a container.

FAQ

Why does 'No such file or directory' appear when the file exists?

Usually the shebang points to a missing interpreter, the script has CRLF line endings, or a 32-bit binary is missing its loader. 'file script' and 'head -1 script' help identify it.

How do I fix ENOENT in Node.js?

Build paths with path.join(__dirname, 'file') instead of relative paths, and check the file exists in the deployed build.

Related errors

Source: Linux kernel errno.h · fixes written and reviewed by the editors (see our editorial policy)

Last updated: