Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm command - sudo or not?

Tags:

Currently I always run sudo npm install <package-name> but as I understand it's not correct.

I want to have opportunity not to use it as root/Administrator. I followed some advice and used this command sudo chown -R <username> ~/.npm but it won't work...

for example, it's an output of my npm install jade

... npm http 200 https://registry.npmjs.org/amdefine npm http GET https://registry.npmjs.org/amdefine/-/amdefine-0.0.5.tgz npm http 200 https://registry.npmjs.org/amdefine/-/amdefine-0.0.5.tgz npm ERR! Error: EACCES, symlink '../jade/bin/jade' npm ERR!  { [Error: EACCES, symlink '../jade/bin/jade'] errno: 3, code: 'EACCES', path: '../jade/bin/jade' } npm ERR!  npm ERR! Please try running this command again as root/Administrator. 

as you see download started successfully but then failed..

I'm wondering what is the best way to disallow sudo on npm?

like image 292
Kosmetika Avatar asked May 23 '13 21:05

Kosmetika


People also ask

Do I need sudo for npm?

npm . Just always use sudo -i or sudo -H when running npm install to install global packages and your npm permissions problems will melt away.

Does npm install need root?

npm installs packages locally within your projects by default. You can also install packages globally (e.g. npm install -g <package> ) (useful for command-line apps). However the downside of this is that you need to be root (or use sudo ) to be able to install globally.

Which user does npm run as?

The npm install runs as root in the container, and since npm runs package-defined scripts, it has a protective mechanism to avoid running them as root, it drops its privileges to "nobody".


2 Answers

It's possible (and advisable) to npm install -g node modules without sudo.

Check the permission of your /usr/local/share/npm/bin folder. I had installed node and npm through brew (without sudo) and that particular folder ended up being owned by root.

This fixed it for once and for all:

$ sudo chown $(whoami) /usr/local/share/npm/bin 

(As for disallowing sudo with npm: you'd have to tweak npm for that. Your own node code could make use of https://npmjs.org/package/sudo-block, npm install sudo-block)

EDIT: even though this works, I no longer use -g. Instead use prefix (see next answer), or better yet use NIX https://unix.stackexchange.com/a/381797 (even on OSX)

like image 163
lionello Avatar answered Sep 23 '22 15:09

lionello


In my opinion is the cleanest way to specify the npm prefix:

npm config set prefix ~/.node_modules 

And then to add the following to you .bash_profile

export PATH=$HOME/.node_modules/bin:$PATH 

Now the packages will install into your user directory and no permissions will be harmend.


EDIT: If you can't install yeoman, create a bash file in one of your PATH directories named yodoctor with the following contents

#!/bin/bash yo doctor 

Make the file executable with

chmod +x yodoctor 

And now you should be able to install yeoman.

like image 23
febLey Avatar answered Sep 21 '22 15:09

febLey