Install Node.js and npm with apt
Unlike other installation routines, npm is not part of Node.js for package manager installations. Therefore, we will install both separately.
As usual, we install Node.js with apt
:
__$ sudo apt install -y nodejs
With the following command we can query the installed version of Node.js:
__$ node -v
A version number should appear as output, otherwise something went wrong during installation.
We proceed similarly for npm. First the installation with apt
:
__$ sudo apt install -y npm
And testing by the version output:
__$ npm -v
Reconfigure npm for global packages
Those who did not follow the tutorial and run everything as root
user can skip the following section.
With npm there is the possibility to install modules globally, which can then be used system-wide. Since we installed Node.js as root
user, but created another user tom
for security reasons, we need to change the location of the global modules and create an environment variable. This allows tom
to create global modules in his user directory. We avoid access problems with this, because otherwise the packages would only be accessible with sudo
.
First, we create a hidden folder for npm packages underneath the home directory of tom
:
__$ mkdir -p ~/.npm/lib
(In Linux, folders and files whose names begin with a period (.
) are not displayed. They can be output with the -a
parameter: ls -a
)
We change the default path for global npm packages with the npm config set
command:
__$ npm config set prefix ~/.npm
We add an environment variable to the current user's Bash configuration file .bashrc
:
__$ echo 'export PATH="$PATH:$HOME/.npm/bin"' >> ~/.bashrc
And let the system re-read the bash configuration file:
__$ . ~/.bashrc
With these settings, Node.js first looks for modules inside the project in the node_modules
directory and if nothing is found there, in the home directory under ~/.npm
.
The actual location would otherwise have been /usr/local/npm
.
Alternatively, we could have shared the npm system folder /usr/local/npm
or parts of it to tom
, but this would lead to chaotic conditions sooner or later.