Zander
Zander

Reputation: 2684

Why does npm need sudo for EVERYTHING?

I don't know how I've managed it but npm seems to need sudo for absolutely every command, even npm help does not work without sudo. If I use a command without sudo, I do not see am EACCESS error, but instead my terminal session hangs and then just closes that tab (I use iTerm on Mac).

I have tried changing the ownership of my local .npm folder, outlined here and also done the same on my /usr/local/bin folder where node is installed but none of these allow me to just run npm without sudo, even when installing local packages...! It seems to me that something has screwed along the way, can anyone help?

Many thanks

Upvotes: 5

Views: 7614

Answers (4)

Kalhara Tennakoon
Kalhara Tennakoon

Reputation: 1482

Use the below option.

Open the terminal and cd to your Home directory and run the below command.

mkdir "${HOME}/.npm-packages"

Then this command after that.

npm config set prefix "${HOME}/.npm-packages"

Next, open your .zshrc file using the open -t .zshrc command and add the following to it.

NPM_PACKAGES="${HOME}/.npm-packages"

export PATH="$PATH:$NPM_PACKAGES/bin"

# Preserve MANPATH if you already defined it somewhere in your config.
# Otherwise, fall back to `manpath` so we can inherit from `/etc/manpath`.
export MANPATH="${MANPATH-$(manpath)}:$NPM_PACKAGES/share/man"

Upvotes: 1

Zhu Xiaohu
Zhu Xiaohu

Reputation: 639

Take ember project for example, I give all related project directory root:

neil@neil-System-Product-Name:~/Projects/ember-quickstart$ sudo chown -R $(whoami) /home/neil/Projects/ember-quickstart/ neil@neil-System-Product-Name:~/Projects/ember-quickstart$ ember s Could not start watchman Visit https://ember-cli.com/user-guide/#watchman for more info. Livereload server on http://localhost:7020 Build successful (10679ms) – Serving on http://localhost:4200/ Slowest Nodes (totalTime => 5% ) | Total (avg)
----------------------------------------------+--------------------- Babel (18) | 7561ms (420 ms)
Concat (8) | 1872ms (234 ms)
Rollup (1) | 629ms

Upvotes: 0

codeMonkey
codeMonkey

Reputation: 406

I encountered the same error after a fresh install of 0.12.4 today; this solved the problem for me:

sudo chown -R $(whoami):admin /usr/local/lib/node_modules

In my particular case, I noticed that this folder was owned by '{some-large-integer-account}:wheel'...YMMV

If that doesn't solve it, take a look at the ownership of the folders that are being blocked as mentioned in the EACCESS error trace. If you're not sure what the ownership should be, you can usually infer it from the sibling dirs' ownership.

Upvotes: 7

Tom Grant
Tom Grant

Reputation: 2045

I had this as well on my machine. What I did to fix it (there are probably much less extreme ways) was to completely remove npm, and then did a fresh installation node.js (with which npm is included) from http://nodejs.org/ making sure I didn't install as root. That then allowed me to use npm without root (except for global installs).

Upvotes: 0

Related Questions