2

I'm trying to get set up with npm and nodejs on ubuntu server but noticed that somehow I ended up with two versions after running sudo apt-get install nodejs npm I know this because when I run npm -v, it returns 1.3.10, while when I run sudo npm -v, it returns 2.3.0. I'd like have this set up properly before I go any further. Does anyone understand why I ended up with two versions? It's my understanding that installing nodejs should also install npm, but this only allows me to run npm with elevated privileges which I know I shouldn't be doing. Can anyone let me know what I'm doing wrong? Thank you.

user2027202827
  • 1,234
  • 11
  • 29

4 Answers4

2

It appears that the package npm includes nodejs and nodejs includes npm. At the very least, they include different versions of npm (I didn't check the node versions) which then conflict. It turns out that nodejs includes the more recent version of npm (and I'd assume a more recent version of node), so that's the one I chose to proceed with. Here's the full solution that worked for me.

Uninstall all versions of node and npm. Then run sudo apt-get install nodejs. Lastly, reboot the system.

After rebooting, npm can be run without elevated privileges, but still has trouble installing without them. To fix this, the advice in this answer is useful. In short, create the following configuration file to store packages in a different location:

#~/.npmrc
prefix=~/.npm_modules

I'm not completely sure what part rebooting plays, but it appears some memory of the npm package is retained after uninstalling it. Before a reboot, ubuntu continues to look for npm in /usr/bin/ even though it resides in /usr/local/bin/. I would imagine this has something to do with the order of directories in the PATH environmental variable, but if anyone can fill in what I'm missing here, I would appreciate it even if only for informational purposes.

EDIT

After making this post, it occurred to me that changing the location of npm packages might pose a problem for updating npm with npm install -g npm which I think is the suggested method to make the update. An alternate solution I've seen is to make the current user the owner of the default package location, but this seems a little silly to me and wouldn't work for multi-user setups. Does anyone know of a better solution? This whole npm setup seems a bit of a catch-22...

Community
  • 1
  • 1
user2027202827
  • 1,234
  • 11
  • 29
1

Try running which npm and then sudo which npm to see which is considered the global installation and remove the other one. Then point your environment to the existing npm.

taco
  • 1,367
  • 17
  • 32
  • Actually I tried that already but I don't know how to tell which is global. I also don't think `sudo which npm` is giving what that command intends. Both those which commands point to the same version of npm (/usr/local/bin/npm). Is there a way to apply the `sudo` to `npm` instead of both `which` and `npm`? – user2027202827 Jan 29 '15 at 22:52
  • sudo su to root and just run `which npm` to be sure. – taco Jan 30 '15 at 08:42
  • @hobenkr did you figuire it out? – taco Feb 02 '15 at 18:58
  • sorry for the latency in response. I'm not sure I completely figured it out but Sam's advice seems to be the most advisable to other users. From what I've read (and found by experimentation), installing node from source sounds like the preferred way obtain this software, though I'm sticking with the method I posted for now since it works for the time being and I'm only using it temporarily for development. – user2027202827 Feb 24 '15 at 01:15
  • I would say installing from source is fine for your personal workstation, but I wouldn't recommend it in a production environment, unless you plan to maintain the installation. – taco Feb 24 '15 at 05:33
1

For some Linux distributions (Debian/Ubuntu and RedHat/CentOS), the latest node version provided by the distribution may lag behind the stable version. Here are instructions from NodeSource on getting the latest node.

Then you can install the latest npm by running sudo npm i -g npm@latest.

Sam Mikes
  • 10,438
  • 3
  • 42
  • 47
1

This is an older question, but I recently had a similar problem on windows where I had node js installed and a gradle script controlling my build. npm --version on the command line reported the correct version of npm, but the gradle task got a different one. I had previously run the npm -g npm@latest command, which is how I got myself in trouble.

As it turns out, the "other" version of npm was hosted in my user folder in npm-cache, and once I deleted that folder my gradle task only found my installed copy of npm.