How to fix the "Missing write access" error when using npm
Quickly solve this annoying problem when installing global packages using npm
The first time you try to install a package globally using npm, using the syntax npm install -g <package> on a Mac, or Linux, you might get a weird error, saying something like
Missing write access to /usr/local/lib/node_modules

or something along those lines, followed by a long list of other errors of warnings, a consequence of the first error that’s printed to you.
This error is preventing us to install the package.
How do you fix this? It’s a permission error, which means you don’t have write access to that folder.
This is how to solve it. Run this command:
sudo chown -R $USER /usr/local/lib/node_modules
Let’s break it down:
sudo means we are running this command as root, the system super user. This is because we don’t have permission to write to that folder, but root will be able to fix any permission. This command also means the system will ask for your password to confirm.
chown is the command we use to change the owner of a file or folder. We set the -R option to change the owner recursively, so we also get owner access to all the files already contained in there.
$USER is an environment variable automatically set to your username.
And the final piece is the folder path.
Running this path will make the folder yours, so you can safely run your npm install -g <package> commands!
Pay attention to the folder listed by the error message. If it’s different, update the chown command accordingly.
This tip applies to single user systems. On a multi-user system, you might want to create a dedicated directory for npm modules, see https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally.
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.