Fix it fast
Most likely: The current user cannot write to the path shown in the error, commonly the project node_modules, npm cache, or global install prefix.
1. Confirm this is your error
npm ERR! code EACCES
npm ERR! Error: EACCES: permission denied
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/package' 2. Check the cause
npm config get cache
npm config get prefix
ls -la <failing-path>
whoami 3. Apply the safe fix
# Project install: fix ownership of the project and npm cache for the current user.
sudo chown -R $(whoami) .
sudo chown -R $(whoami) ~/.npm
npm install
# Global installs: prefer a user-writable npm prefix instead of sudo.
npm config set prefix ~/.npm-global 4. Verify it works
npm install
npm config get prefix
npm config get cache Don't use unsafe shortcuts
- Do not use
sudo npm installin a project as a normal fix, it often creates more root-owned files. - Do not chown broad system directories like
/usror/usr/localwithout understanding the impact. - Do not remove
node_modulesor cache data until you know the failing path and ownership problem.
What This Error Means
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
The fastest fixes here come from checking the immediate failing layer before you change anything unrelated. Make one correction at a time and re-test from the same environment.
Identify the path npm is failing on (look for the last referenced file path in the error output).
Avoid sudo npm install for project installs. It often causes mixed ownership inside node_modules.
Fix ownership of npm cache and project folder (example:sudo chown -R $(whoami) ~/.npm).
If the error is for global installs, use a user-writable prefix (example:npm config set prefix ~/.npm-global) and ensure PATH includes it.
Retry after cleaning local state when safe (common:remove node_modules and retry install).
Why It Happens
The current user does not have permission to write to the project folder, npm cache, or global prefix.
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
Check ownership/permissions for the failing path:ls -la <path>
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/lib/commands/cache.js
This is a representative filesystem write path during npm operations. Filesystem codes like this are raised by Node/OS when this write fails. - GitHub
break
}
output.standard(`Deleted: ${key}`)
await cacache.rm.entry(cachePath, key)
// XXX this could leave other entries without content!
await cacache.rm.content(cachePath, entry.integrity)
}