Victor Leung
Victor Leung
BlogAI SolutionAlphaAlgoFlower shopFX CombineIEESushi ClassifierWealth Agile

Common npm permission issue

February 20, 2015

The problem:

If you are using mac and install nodejs via pkg file downloaded from the official website, you are likely to encounter this error message when you try to install a npm module globally:

npm ERR! Please try running this command again as root/Administrator.

My solution

DO NOT use sudo command to install the package!

sudo npm install module -g

Some people suggest the above solution on Stack Overflow, but I strongly encourage you not to do package management with sudo. This hack may temporarily solve your problem, but you will encounter more problems later on.

Here is the recommanded way:

Step 1: Find out your username with this command:

whoami

For example, my username is victorleungtw

Step 2: Change ownership of the node modules folder:

sudo chown -R `whoami` /usr/local/lib/node_modules

Then you won’t ever have to use sudo when you install npm packages.


About Victor Leung

Software development professional with expertise in application architecture, cloud solutions deployment, and financial products development. Possess a Master's degree in Computer Science and an MBA in Finance. Highly skilled in AWS (Certified Solutions Architect, Developer and SysOps Administrator), GCP (Professional Cloud Architect), Microsoft Azure, Kubernetes(CKA, CKAD, CKS, KCNA), and Scrum(PSM, PSPO) methodologies.

Happy to connect
LinkedIn
Github
Twitter
@victorleungtw

Continuous improvement

Copyright © victorleungtw.com 2023.