The proper way to install node on Linux, with nvm

Submitted by John on Mon, 03/18/2019 - 21:23
A plate containing a dozen cupcakes. The cupcakes are covered in fun-fetti icing, and decorated with candy eyeballs and moustaches to look like faces. Each cake's eyes and faces look sightly different, giving each one a unique expression, while appearing to look in different directions.

Little things can take a long time. If you are like me, and use Linux (Ubuntu) for your work machine, you might inevitably run into a permissions issue with the default way npm/node is installed. 

npm WARN checkPermissions Missing write access to /usr/local/lib/node-v10.14.2-linux-x64/lib/node_modules

The problem is that the default location where npm places globally installed packages is under usr/local/lib, which of course is a root-owned directory. The best way to get around this is to install node/npm/node modules in a directory your user owns. The best way to do this is with Node Version Manager (nvm). I found a nice guide from Dean McDonnell at NearForm

Here is a full example of one of the permissions errors I am talking about. 

$ npm install -g generator-web-starter
npm WARN deprecated gulp-util@3.0.8: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5
npm WARN deprecated samsam@1.3.0: This package has been deprecated in favour of @sinonjs/samsam
npm WARN checkPermissions Missing write access to /usr/local/lib/node-v10.14.2-linux-x64/lib/node_modules
npm ERR! path /usr/local/lib/node-v10.14.2-linux-x64/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node-v10.14.2-linux-x64/lib/node_modules'
npm ERR!  { [Error: EACCES: permission denied, access '/usr/local/lib/node-v10.14.2-linux-x64/lib/node_modules']
npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, access \'/usr/local/lib/node-v10.14.2-linux-x64/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node-v10.14.2-linux-x64/lib/node_modules' }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/jbrandenburg/.npm/_logs/2019-03-18T14_59_31_417Z-debug.log

And it’s not a best practice to install node packages as root. You can set yourself as the owner of the /usr/local/lib/[your node version]/lib/node_modules, but this will require re-setting permissions when you update node. Following the instructions from Dean McDonnell at NearForm, be sure to remove all previous instances of node/npm and your node-modules. If you don’t want to make that leap just yet, you can rename these files/directories with something like “-bak”, and that should at least remove the relevant files from your path. Once you have all that done, you really just need to install nvm, follow the instructions at the nvm project. Speaking of paths, be sure to remove/comment out any instances of node and node related variables from your ~/.bashrc and ~/.profile files. You may additionally need to remove/comment out the contents of /etc/profile.d/nodejs.sh 

nodejs.sh:

NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
export NODE_PATH


Don’t worry about actively setting NODE_PATH to anything, that seems to be sorted out when you invoke any sort of node command once nvm is installed. To test this, try installing yeoman and running the doctor command: 

$ npm install -g yo

$ yo doctor

Yeoman Doctor
Running sanity checks on your system

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

Everything looks all right!


And there you have it. A working nvm installation. 
 

Update, if you happen to still be using grunt, you might notice that you have uninstalled grunt in this process. The next time you try to run a grunt command, it will prompt you with the following:

Command 'grunt' not found, but can be installed with:

sudo apt install node-grunt-cli

But do not follow these instructions! You will want to install grunt with npm:

npm i -g grunt-cli

Which will install grunt in the node directory specific to your active version in nvm. 

 

Tags