Introduction
NodeJS is a runtime environment to execute JavaScript code outside the web browser.
Continuing with the earlier discussion, after successfully installing node and npm, I went on to install "http-server" from npm
globally only to get this EACCESS
error.
npm -g install http-server
In this blog, first I'll install Node and npm system wide. Then I'll tell npm
where to install global packages. Next, I will install n
to manage multiple versions of node.
Installing Node and npm globally
First update the local package list to see if there's any update available using apt-get
sudo apt-get update
Then install nodejs
and npm
packages
sudo apt-get install nodejs npm
To verify the install, check their versions
node -v
v18.7.0
Configuring global install paths for npm
npm
uses the prefix
option to set its global install path. I can set this to a directory inside my home. Being inside my home directory, this gets rid of that EACCESS
error when installing global packages as I have read/write access to a directory inside my home folder.
First I will cd
into my home directory
cd ~
Then create a folder named "global-node-modules"
mkdir global-node-modules
Now to install any global package to this directory, we can use that prefix
option like so
npm --prefix=$HOME/.global-node-modules install -g http-server
I can specify this option as default in .npmrc
file in my home directory so that I don't need to specify it every time I'm doing npm install -g
. My .npmrc
file looks like this.
prefix=/home/shyam/.global-node-modules
Now that the prefix is set, I need to include the modules to my PATH
so that I can run the globally installed modules from anywhere. For this, add
export PATH="$HOME/.global-node-modules/bin:$PATH"
to the end of your .bashrc
, .zshrc
or equivalent file. After all, the modules will be located inside the .global-node-modules/bin
folder.
`n` - Managing node versions
Installing `n`
Now that npm
is available after installing node and npm in the first step, I can install n
globally using npm
.
npm install -g n
n
also like npm
tries to install packages to /usr/local
by default. This behaviour can be changed by updating the N_PREFIX
to a folder inside the home directory.
I can set it to a directory inside home like .n
using export N_PREFIX=$HOME/.n
. This line also need to be added to the .bashrc
or similar configuration file for your shell.
After doing this, I must also add the N_PREFIX/bin
folder to PATH
to be able to execute the current version of node selected using n
.
Thus if you are using bash, the following lines need to be added to .bashrc
file
...
export N_PREFIX="$HOME/.n"
export PATH="$PATH:$N_PREFIX/bin"
Using `n`
I can now install the latest and lts version of node using n
n latest
installing : node-v19.4.0
mkdir : /home/test/.n/n/versions/node/19.4.0
fetch : https://nodejs.org/dist/v19.4.0/node-v19.4.0-linux-x64.tar.xz
copying : node/19.4.0
installed : v19.4.0 to /home/test/.n/bin/node
active : v18.7.0 at /usr/bin/node
n lts
installing : node-v18.13.0
mkdir : /home/test/.n/n/versions/node/18.13.0
fetch : https://nodejs.org/dist/v18.13.0/node-v18.13.0-linux-x64.tar.xz
copying : node/18.13.0
installed : v18.13.0 to /home/test/.n/bin/node
active : v18.7.0 at /usr/bin/node
and change the active node version to the latest one by simply executing n
and selecting from the list of installed versions
ο node/18.13.0
node/19.4.0
Use up/down arrow keys to select a version, return key to install, d to delete, q to quit
At this point, you're all setup for developing node applications and installing npm packages!