Common npm Permission Issues
The Problem:
If you are using a Mac and have installed Node.js via a pkg file downloaded from the official website, you are likely to encounter the following error message when you try to install an npm module globally:
npm ERR! Please try running this command again as root/Administrator.
My Solution:
DO NOT use the sudo
command to install the package!
sudo npm install module -g
Some people recommend the above solution on Stack Overflow, but I strongly advise against managing packages with sudo
. This workaround may temporarily solve your problem, but you’ll likely encounter more issues later on.
Here is the recommended approach:
Step 1: Determine your username with the following command:
whoami
For example, my username is victorleungtw.
Step 2: Change the ownership of the node modules folder:
sudo chown -R `whoami` /usr/local/lib/node_modules
After performing these steps, you won’t have to use sudo
when installing npm packages in the future.