What This Error Means
npm hit ENOTDIR because it expected a directory in the path, but a file exists there instead, breaking the package or cache lookup.
Read this as a precise clue about which part of the workflow broke first. Once you know the failing layer, the fix path gets much shorter.
How to Fix It
Identify the path npm is failing on (look for the last referenced file path in the error output).
Make sure you are running npm in the right directory (the one with package.json).
If the missing path is under node_modules, remove node_modules and reinstall.
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).
Verify the Fix
Re-run the original command and confirm the filesystem error no longer appears.
If this is a permission fix, confirm new files in node_modules are owned by the expected user.
Manual filesystem checks
Confirm package.json exists in the current directory: ls -la package.json.
Examples
npm ERR! code ENOTDIR How npm writes files during install
This is the part worth understanding if the quick fix did not hold. It explains what npm is trying to do at the moment the error appears.
npm is reporting a failure at a specific layer of the workflow:local environment, configuration, remote service access, or artifact metadata.
The fastest path is to identify which layer broke first, then verify that layer directly instead of retrying the same high-level command and hoping for a different result.
Prevent It From Coming Back
To prevent this, 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, and ensure CI runners have enough disk space and sensible file descriptor limits.
Docs and source code
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,
})