Node modules have 755 permissions, what permissions should I set so that npm don't require sudo?
You don't need to alter permission of npm
to be able to use it without sudo
. TO set up npm
to run without sudo
follow these steps:
Option 1:
-
Get path of npm directory:
npm config get prefix
-
If you got
/usr
go to option 2 else change owner of npm directories to yours:sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
Option 2:
-
Create directory for global installations:
mkdir ~/.npm-global
-
Configure to use that directory:
npm config set prefix '~/.npm-global'
-
Open or create
~/.profile
and add this:export PATH=~/.npm-global/bin:$PATH
-
Add this also to your
~/.bashrc
file (optional) :export PATH=/home/username/.npm-global/bin:$PATH export NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript:/home/username/.npm-global/lib/node_modules
-
Refresh the files:
source ~/.bashrc
-
Test by running:
npm install -g jshint
source:
https://docs.npmjs.com/getting-started/fixing-npm-permissions
http://www.competa.com/blog/how-to-run-npm-without-sudo/
Related videos on Youtube
Indu Pillai
Updated on September 18, 2022Comments
-
Indu Pillai over 1 year
I'm using a framework which runs
npm
andwebpack
automatically, the directory/usr/lib/node_modules
have 755 permissions which require to runnpm
throughsudo
. The framework I use doesn't allow addingsudo
to the command.Now I need to install npm packages in global form (e.g.,
npm install webpack -g
) without usingsudo
.What permissions will be OK for
/usr/lib/node_modules
? If I set 777 permissions will that be OK and secure?Thank You!
-
steeldriver over 7 yearsPresumably related to I made /usr/local/ world writable, is it secure?
-
muru over 7 yearsEasier to use ACLs (like in my answer here: askubuntu.com/a/867337/158442, just change username and path to values suited to your case)
-
-
Indu Pillai over 7 yearsA nice detailed answer, let me try it!
-
George Udosen over 7 yearsThat's how I use npm on my system.
-
Indu Pillai over 7 yearsI'm using Zshell, so I need to add the stuff to ~/.zshrc in the step 4 ?
-
Indu Pillai over 7 yearsAnd do I need to change it for Zshell, or it will work as such?
-
George Udosen over 7 yearssure that is right.
-
George Udosen over 7 yearsAdd it to that shell too.
-
George Udosen over 7 yearsPlease remember to source those files after adding.
-
Indu Pillai over 7 yearswhat do you mean by "to source those files" ?
-
Indu Pillai over 7 yearsYou meant keep the backup of the dot files ?
-
George Udosen over 7 yearsIs just to refresh the file like this
source ~/.bashrc
orsource ~/.zshrc
... -
Indu Pillai over 7 yearsOh. :) I restarted the terminal and it worked.
-
George Udosen over 7 yearsok same thing then cheers :).
-
Ilmari Karonen over 7 yearsYou really should replace "if you got
/usr
" with "if you got anything that isn't under your home directory". And, since anything under your home directory is almost certainly owned by you anyway, you might as well leave option 1 out entirely, and just skip straight to option 2. -
Brady Dean over 7 yearsShould the path be set in both files?
-
Ilmari Karonen over 7 yearsAlso, step 4 (modifying
.bashrc
) in your second option should be unnecessary: your.profile
will be run automatically when you first log in, and any changes toPATH
made in it will be inherited by subshells, so repeating the changes in.bashrc
is redundant and will just needlessly bloat yourPATH
with duplicate entries. (You probably do want to add something likeexport NODE_PATH=~/.npm-global/lib/node_modules:$NODE_PATH
to.profile
, though.) -
George Udosen over 7 years@ilmariKaronen yes step 4 is not needed, updated answer to reflect that.
-
George Udosen over 7 years@BradyDean no it should not, updated OP to reflect that.