(Re)Installing Node is a Pain

08/01/2015 Sean Cole Comment 1

I’ve been using nodejs and npm on Ubuntu 14.04 for a while now, but in a limited capacity. I haven’t been using grunt or gulp, or any other task runner, just npm. After watching a video, Master Front-End Workflow with Yeoman, Grunt and Bower, I decided that I wanted to start taking advantage of some of these tools.

The trouble started almost immediately.

$ sudo npm install -g yo
✖ Node.js version

Simple enough. I just need to update my installation of node… Maybe it’s just me, but this seems more complicated than it should be.

  • There are at least three different upgrade/install paths, most of which are wrong
  • Packages installed as root are inaccessible, but not always
  • The binaries don’t initially register to the PATH

Uninstalling nodejs

Long story, short, I ended up having to uninstall nodejs, npm, and all of my packages in order to install nodejs, npm, and all of my packages.

Let me backtrack a little to illuminate the cause. For a while now I have had to install packages as root by using sudo npm install -g [package]. If I didn’t I would get permission issues. It was a mild annoyance, and I didn’t really think much of it, I just thought it was a quirk of node. But now yeoman can’t use one of my packages because it doesn’t have root access, not even when running it as root.

$ sudo yo webapp
Error webapp 

You don't seem to have a generator with the name webapp installed.
You can see available generators with npm search yeoman-generator and then install them with npm install [name].
To see the 0 registered generators run yo with the `--help` option.

So it’s time to uninstall everything, just to make sure I don’t run into this kind of issue again. First, uninstalling node is fairly easy.

$ sudo apt-get remove nodejs

However, in order to remove all node packages and configurations, you have to manually remove each of the corresponding folders. Luckily, I was able to find a quick and dirty way from a Stack Overflow answer, Completely uninstall nodejs and reinstall from beginning.

$ sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}

Reinstalling nodejs

Alright, I’m now starting from a clean slate.

As I mentioned earlier, there are at least three install/upgrade options for nodejs. However, only one of them will get you the latest and greatest version of nodejs.

Installing from the PPA, ppa:chris-lea/node.js, repository gave me the same version I already had (v0.10.37). Come to find out, PPA’s are notorious for this.

Trying David Walsh’s solution of upgrading from n didn’t work.

Instead, I ended up using the cURL solution.

$ curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -*
$ sudo apt-get install -y nodejs
$ node -v
bash: /usr/local/bin/node: No such file or directory

$ nodejs -v

$ npm -v
bash: /usr/local/bin/npm: No such file or directory

$ which npm

Awesome, it’s installed, but there appears to be some issues. At least I’m familiar with these. In the past I’ve fixed this by manually creating a symlink to nodejs and npm (sudo ln -s `which nodejs` /usr/local/bin/node). However, this is hackey, instead, I recently found a solution suggesting that clearing bash’s hash table would fix it.

$ hash -r
$ node -v

$ npm -v

And it does.

Installing a package

Let’s try installing yeoman again.

$ npm install -g yo
npm ERR! Please try running this command again as root/Administrator.


So there are a few things I tried here. I’m not sure if it is a combination of all of them, or just the last one that fixed it.

$ sudo chown -R $(whoami) ~/.npm
$ sudo chown -R $(whoami) /usr/local/lib/node_modules
$ echo 'prefix = ~/.node' >> ~/.npmrc

I’m pretty sure it was just the last one, adding the prefix, that did it. This was from an answer on Stack Overflow about modifying the npm install directory so that packages would not be installed inside the /usr directory. The reasoning he gave is sound, and after that it started working. Sort of…

$ npm install -g yo
✖ NODE_PATH matches the npm root
npm global root value is not in your NODE_PATH

[Info] NODE_PATH = /usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript npm root = /usr/local/lib/node_modules

[Fix] Append the npm root value to your NODE_PATH variable
Add this line to your .bashrc
    export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules
Or run this command
    echo "export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules" >> ~/.bashrc && source ~/.bashrc

Found potential issues on your machine :(

At least this time it tells us how to fix it. Right?

Almost. It suggests adding the old packages installation path to the NODE_PATH environment variable. However, we just moved the installation path in the previous step so that we wouldn’t have to install packages as root. Adding the new installation path should fix this.

$ echo "export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules:~/.node/bin" >> ~/.bashrc && source ~/.bashrc

One last time.

$ npm install -g yo
Running sanity checks on your system

✔ Global configuration file is valid
✔ Node.js version
✔ No .bowerrc file in home directory
✔ No .yo-rc.json file in home directory
✔ npm version
✔ NODE_PATH matches the npm root

Everything looks all right!

And does it work?

$ npm install -g generator-webapp
$ yo webapp
yo: command not found

One final tweak should fix it. Just have to add the new installation path to the PATH environment variable.

$ echo "export PATH=$PATH:~/.node/bin" >> ~/.bashrc && source ~/.bashrc
$ yo webapp
? 'Allo Sean! What would you like to do? (Use arrow keys)
  Run a generator
❯ Webapp


Yeah… I don’t think it’s just me. This was entirely too convoluted.

Just to make sure this all worked, I completely uninstalled and followed all of the above instructions without a hitch. The only issue I have found with the above instructions is that running npm list will always return empty unless you are in the ~/.node/lib directory.

Thankfully yeoman added those sanity checks. No other package I have thus far installed has done that, so I have been setting myself up for this disaster for a while.

You Might Also Enjoy:


Building My First Webapp with Yeoman | DEV1UP 2015-08-02 13:02:32

[…] (Re)Installing Node is a Pain 08/01/2015 […]

Join the Discussion

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax