3

I've installed nodejs + npm on my Ubuntu machine using the following commands:

curl -sL https://deb.nodesource.com/setup | bash -
apt-get install -y nodejs

And, in order to use yeoman without sudo I used the following commands:

echo prefix = ~/.node >> ~/.npmrc
export PATH="$PATH:$HOME/.node/bin" 

After that, I can't update the NPM. If I run npm update -g npm the version number doesn't change, but, if I run the update command before the echo prefix command, the update works and npm is updated.

Community
  • 1
  • 1
  • what is the output of the following command: `which npm` – Jay Feb 05 '15 at 20:06
  • @JevZelenkov the output is `/usr/bin/npm` –  Feb 05 '15 at 20:19
  • Could you run the following command and say where does the symlink point to `ls -l /usr/bin/npm`? – Jay Feb 05 '15 at 21:02
  • @JevZelenkov I ran the commant and this is the output: `lrwxrwxrwx 1 root root 38 Jan 28 19:03 /usr/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js` –  Feb 05 '15 at 21:19
  • check out the updated answer and let me know if it works. If not, let me know what error messages are you getting. It also might be a good idea to close and open your terminal window again to get reset your $PATH environment variable back to normal – Jay Feb 05 '15 at 21:35
  • 1
    I've tried out all path changes, reset, close and reopen but not works (dont chage the version). But I saw a new thing. if i run `npm install -g npm` close and reopen the terminal the version number is updated! The output of the new install: `/home/nicolocodev/.node/bin/npm -> /home/nicolocodev/.node/lib/node_modules/npm/bin/npm-cli.js npm@2.4.1 /home/nicolocodev/.node/lib/node_modules/npm` –  Feb 05 '15 at 22:10
  • yep, I've said that in my answer: "npm is written in nodejs and is managed via npm after the initial install." Just added an update command to make it clear – Jay Feb 06 '15 at 07:34

2 Answers2

1

update

You have node + npm installed. By default npm uses /usr/lib/node_modules/ directory to install global modules. Non-priveledged users normally don't have write access to that directory and as such, cannot install npm packages globally.

The command echo prefix = ~/.node >> ~/.npmrc tells npm to install global packages to ~/.node/node_modules instead of usr/lib/node_modules.

After calling:

echo 'export PATH=$HOME/.node/bin:$PATH' >> ~/.bashrc

all npm packages which provide binary scripts are added to $PATH (e.g. yo, browserify) but also npm.

npm package is managed via npm package manager itself. The following command updates npm to the latest version:

npm install -g npm

previous answer

NodeSource provides a binary build of nodejs + npm.

In usage instructions they say to run both commands as admin for Debian systems:

sudo curl -sL https://deb.nodesource.com/setup | bash -
sudo apt-get install -y nodejs nodejs-legacy

The most important line in the setup script is this:

 echo 'deb https://deb.nodesource.com/node ${DISTRO} main' > /etc/apt/sources.list.d/nodesource.list

node + npm should be installed on your system globally now. Updates should be managed by apt-get from now on.


From what I can tell, you have another node + npm installed in your ~/.node directory. I am not sure why you need it. As far as I know global npm packages are installed into ~/.npm directory and they don't interfere with npm binary installed by apt-get.

In any way, if you really want to use your custom node installation from ~./node/bin, you should export $PATH this way:

export PATH="$HOME/.node/bin:$PATH"

Also you can export $PATH automatically by adding this command to ~/.bashrc file:

echo 'export PATH=$HOME/.node/bin:$PATH' >> ~/.bashrc

*NIX looks for binary files (e.g. npm) in each directory specified in $PATH. It goes from left to right and executes the first matching binary file it finds. Somewhere in $PATH variable you have /usr/bin. If you want to npm / node from ~/.node/bin to be found first, you should put that directory further to left in the $PATH environment variable.

Jay
  • 3,445
  • 2
  • 24
  • 29
  • Ok, that was my way: `echo 'export PATH=$HOME/.node/bin:$PATH' >> ~/.bashrc` then: `npm install -g npm`. Thanks for your help –  Feb 06 '15 at 14:43
0

If you want a well updated nodejs + npm :

sudo apt-add-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
sudo npm update -g npm

Et voilà!

Nurza
  • 201
  • 2
  • 10