Error Knowledge Base NPM ENOTDIR

npm ERR! code ENOTDIR

npm expected a directory but found a file (ENOTDIR).

Affected versions: All supported npm versions.

What This Error Means

npm expected a directory but found a file (ENOTDIR).

How to Fix It

  1. Identify the path npm is failing on (look for the last referenced file path in the error output).
  2. Make sure you are running npm in the right directory (the one with package.json).
  3. If the missing path is under node_modules, remove node_modules and reinstall.
  4. Retry after cleaning local state when safe (common: remove node_modules and retry install).

Why It Happens

  • npm referenced a path that does not exist (wrong working directory, stale node_modules, or a broken install state).

How to Verify

  1. Re-run the original command and confirm the filesystem error no longer appears.
  2. If this is a permission fix, confirm new files in node_modules are owned by the expected user.

Manual filesystem checks

  1. Confirm package.json exists in the current directory: ls -la package.json.

Common CLI Output

npm ERR! code ENOTDIR

Prevention Tips

  • Keep npm cache and project directories owned by the build user.
  • Avoid running project installs as root unless you know exactly why you need it.
  • Ensure CI runners have enough disk space and sensible file descriptor limits.

Where This Can Be Triggered

github.com/npm/cli/blob/417daa72b09c5129e7390cd12743ef31bf3ddb83/workspaces/arborist/lib/arborist/reify.js

Open-source npm CLI code path where this error is raised. - GitHub

        const st = await lstat(node.path).catch(() => null)
        if (st && !st.isDirectory()) {
          debug.log('unpacking into a non-directory', node)
          throw Object.assign(new Error('ENOTDIR: not a directory'), {
            code: 'ENOTDIR',
            path: node.path,
          })

Need help or found a mistake? Contact RepoFlow support for questions.

Join our mailing list