Global installation via npm in JavaScript

Some npm packages are not just libraries, but programs that, once installed, add a new command to terminal.

Such programs should be installed globally. This means that they will not be installed in the node_modules folder of a project, but simply on your computer as a program.

To start the global installation mode, you need to pass the -g flag:

npm install -g somelib

Depending on how you installed NodeJS, you may need to write sudo before a command (usually for Linux):

sudo npm install -g somelib

Let's look at an example. Let's take the sloc utility, which counts the number of lines of code in a project and install it globally:

npm install -g sloc

Being in a folder with any project, we will execute a command in the terminal to count lines, passing the path to the folder as a command parameter (the point is the current folder):

sloc .

Install the cowsay library globally. After installing in a terminal, refer to this library, making a cow to say something in the terminal.

enru