code-quality

Setup Nodejs in Ubuntu

setup nodejs in ubuntu, avoid running npm as root

Setup Nodejs in Ubuntu

This tutorial has been updated with installation instruction for nodejs6.

Installation

To setup nodejs in ubuntu run following command

sudo curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs

First command adds nodejs repo to software sources. This might be helpful in getting update automatically whenever you run sudo apt-get-update. Second command is simply software installation command in ubuntu.

This will also install install npm. Now to install other node packages you can run command like sudo npm install <package name>.

Locating node modules

Location of node_modules may be hard to find. Here is how I found out:

I listed all global node modules with this command: `npm list -g —depth=0’. Output was like:

/usr/lib
├── bower@1.5.3
├── express@4.13.3
├── gulp@3.9.0
├── npm@3.3.12
└── socket.io@1.3.7

First line tells the location of node modules, which was /usr/lib.

Avoid running npm as root

Recently I ran into problem where running npm with sudo command would do nothing and npm command without sudo would not install packages either. Thus I thought of changing owner of node modules to myself so that I need not use npm as root user. Turned out that; not running npm as root user is actually a good practice. So here is what can be done, second method is my preferred one as I don’t format my /home folder when I format or fresh install newer ubuntu versions and I wont need to reinstall global node packages later.

option1: Change the permission to npm’s default directory

sudo chown -R `whoami` /usr/lib/node_modules/

option 2: Change npm’s default directory to another directory

# make a directory for global installations:
mkdir ~/.npm-global
# configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
# Open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
# Back on the command line, update your system variables:
source ~/.profile

Now we no longer need to run npm with sudo command. This can be tested with following command:

npm install -g jshint

References:

About the author

Prakash Poudel Sharma

Engineering Manager · Product Owner · Varicon

Engineering Manager at Varicon, leading the Onboarding squad as Product Owner. Eleven years of building software — first as a programmer, then as a founder, now sharpening the product craft from the inside of a focused team.

Keep reading

More on this

Join the conversation0 comments

What did you take away?

Thoughts, pushback, or a story of your own? Drop a reply below — I read every one.

Comments are powered by Disqus. By posting you agree to theirterms.

0:000:00