3

I am trying to install the latest npm, following the instructions on the npm doc site.

My problem is that using command:

npm install -g npm

to get the latest version of NPM does not seem to work: it does not seem to install any of the needed dependencies, so when I run it I get errors such as:

module.js:340
    throw err;
          ^
Error: Cannot find module 'are-we-there-yet'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/harmic/.node_modules/lib/node_modules/npm/node_modules/npmlog/log.js:2:16)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)

I am using a Centos 6 machine, so I started by installing node + npm from EPEL, like this:

yum install nodejs npm

That went OK. I then went to install the latest npm, as recommended at the above link and many other pages I've seen. My first attempt

sudo npm install -g npm

was not so great, for two reasons:

  1. It installed in /usr/lib/node_modules, over the top of the original RPM installation. I judged this to be extremely poor practice, because the next time I did yum update on the system there was every chance the newly installed files would be overwritten by whatever incoming RPM packages contained, leaving an unholy mess.

  2. It did not work anyway: I could not start npm at all, it constantly complained of missing dependancies as described above.

So I uninstalled the RPMs, removed all the left over files, then reinstalled the RPMs again. This time I created a ~/.npmrc file and put this in it:

prefix = ${HOME}/.node_modules

I also set NODE_PATH environment variable to include that path. After this, npm did install itself in my desired location, but did not install any dependencies:

$ npm install -g npm
npm http GET https://registry.npmjs.org/npm
npm http 304 https://registry.npmjs.org/npm
/home/harmic/.node_modules/bin/npm -> /home/harmic/.node_modules/lib/node_modules/npm/bin/npm-cli.js
npm@3.3.3 /home/harmic/.node_modules/lib/node_modules/npm

Running it from the new installed path gave the error at the top of the page.

Despite thinking that I must be doing something seriously wrong, I set about trying to install the unmet dependencies, one at a time:

npm install -g are-we-there-yet
npm install -g gauge
npm install -g os-tmpdir
npm install -g os-homedir
npm install -g is-absolute
npm install -g asap
npm install -g path-is-absolute
npm install -g builtins
npm install -g validate-npm-package-license
npm install -g concat-stream
npm install -g json-parse-helpfulerror
npm install -g readdir-scoped-modules
npm install -g debuglog
npm install -g lodash._baseclone
npm install -g lodash._bindcallback
npm install -g lodash._baseflatten
npm install -g lodash._baseuniq
...

and still it complains about needing more.

Finally I have concluded that I must have missed something important, so I decided to ask for the help of the SO community...

The initially installed versions of node & npm RPMs are:

  • nodejs-0.10.36-3.el6
  • npm-1.3.6-5.el6

Oh and in case you are wondering, the reason I am trying to update npm in the first place is because when trying to install this package I ran into a build error, the solution to which is apparently to install an updated npm.

harmic
  • 28,606
  • 5
  • 67
  • 91

1 Answers1

3

First of all I would suggest you to forget to install node with RPM/APT etc, the reason for this is because normally they have outdate versions. Infact 0.10 is very old, 0.12 is nearly a year old and now there's 4.1 already. So my suggestion is to use nvm https://github.com/creationix/nvm

nvm is a Node Version Manager. the cool think is that you can switch version with a single command and stay always updated. After installing the node version that you want, you can also upgrade npm.

You can even select a node version for a certain folder in your system (if you have code that runs just in an old version for example)

DevAlien
  • 2,456
  • 15
  • 17
  • Avoiding RPMs may be ok for my development environment, but it won't be feasible when it comes to deployment. I cannot expect sysadmins to learn and manage multiple package management systems, version managers, etc. But I will check out nvm for my dev environment at least. – harmic Sep 18 '15 at 05:33
  • But at this point, in the servers will be their problem to upgrade the version that is not provided by RPM, they can compile ;) – DevAlien Sep 18 '15 at 05:36
  • Using `nvm` is rather easier than trying apt/rpm to get things right. – Muhammad bin Yusrat Feb 18 '20 at 12:10