Home·Error messages

npm · npm

The message

npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'

What it means

npm tried to write into a system directory your user does not own — the package is fine, you simply have no right to write there. It is almost always npm install -g against a node that the operating system's package manager put in a place like /usr/local. npm config set prefix ~/.npm-global moves global installs into your home directory, and once its bin is on PATH the error does not come back. sudo npm install -g does work, but it leaves root-owned files in your cache and node_modules that produce the same EACCES on ordinary installs later; a version manager such as nvm removes the whole class of problem.

The fix

npm config set prefix ~/.npm-global
Printed by
npm
npm
20

With npm the cause sits in the first code XXXX line rather than the last six npm ERR! lines, and when the failure comes from the dependency tree or a native build instead of your own code, deleting node_modules and installing again clears about half of them.

Reading an error message

  • Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
  • If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
  • Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
  • The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
  • Before pasting a fix, check what it throws away. Some of these cannot be undone.

Common questions

Q. What does “npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'” mean?

npm tried to write into a system directory your user does not own — the package is fine, you simply have no right to write there. It is almost always npm install -g against a node that the operating system's package manager put in a place like /usr/local. npm config set prefix ~/.npm-global moves global installs into your home directory, and once its bin is on PATH the error does not come back. sudo npm install -g does work, but it leaves root-owned files in your cache and node_modules that produce the same EACCES on ordinary installs later; a version manager such as nvm removes the whole class of problem.

Q. How do I fix it?

npm config set prefix ~/.npm-global — before running it, check the explanation above for what this command discards.

Q. Which tool prints this?

npm. It sits under npm, and the message runs to 8 words.

Errors nearby