Installing Node.js CLIs locally

Many popular JavaScript development tools like angular-cli and expo-cli recommend installing them globally—using the -g or --global option of npm—in their official documentation. Global installation lets you use the tool anywhere in the machine e.g. after installing angular-cli globally, we can use the ng command anywhere in the terminal, for multiple projects. This convenience, however, is not without any costs—npm -g requires sudo i.e. npm must be run as root for performing global installations. This is required since they are installed to root level directories like /usr/lib/ for which only the root user has the write access. And running npm with sudo is not a very good idea(see: Don’t sudo pip, those arguments are equally applicable to npm). In short, when we use sudo with npm we run arbitrary code from the module’s author(or some malicious actors) as root.
Also, it is better to have our node modules confined to some folder which we can easily remove when not needed.
There are a few ways in which we can avoid sudo npm -g and have a global installation without installing as root. Some are easier than others but none is easier than sudo npm -g that does the job in a single command(apparently it is recommended in documentations for this very reason). Here is a simple approach:

  1. Create a folder for the CLI and do a normal npm install library-name inside it. The CLI executable will be inside this folder.
  2. Find the executable and add it to your $PATH variable so that we can use the CLI command everywhere.

Here is the procedure illustrated for installing Angular CLI in the above manner. The procedure is same for almost all tools.

Make a folder in Home directory(or any other directory of choice)

Say the folder name is: AngularCLI
So, the path to this folder is ~/AngularCLI

Install the CLI using npm inside the created folder

cd ~/AngularCLI
npm install @angular/cli

Find the main CLI script and path to it

Go inside the folder created by npm and find the main js script of the CLI application. In case of angular cli, this file is in ~/AngularCLI/node_modules/@angular/cli/bin/ng

Add source of ng to $PATH so that we can use ng without including the whole path

Open .bashrc file : nano ~/.bashrc
Add source folder obtained earlier to the end of this file:
export PATH=$PATH:~/AngularCLI/node_modules/@angular/cli/bin

Restart the terminal and you can now use the ng command.

Removing and Upgrading

Removing the module is now as easy as just deleting the ~/AngularCLI folder and removing the it from $PATH(i.e. deleting the lines we appended earlier to ~/.bashrc).
For upgrading, just run npm upgrade in ~/AngularCLI or remove the folder and install the latest version of the module using npm install.

Note

If the main script file has file name extensions like .js to the end, add aliases to .bashrc . e.g. If it was ng.js instead of just ng, we need to add alias ng="ng.js" to .bashrc file so that we can use the CLI by calling ng instead of ng.js .


programminggnu/linux
node.jsbashnpmjavascript