JS: Setting up the environment

Theory: Global package installation

Some Node.js packages are not just plug-in libraries, but rather complete programs. For example, there is a JavaScript utility called sloc which can count the number of lines of code in a project. This utility itself is not tied to JavaScript, it works with source files in many different languages.

Such utilities are not associated with a specific project or even JavaScript. The implication is that they can be used by anyone. For these cases, there is another way to install it in npm – global. Take a look at an example:

# Depending on how you install Node.js
# may need sudo at the beginning of the command
npm install -g sloc

The -g flag indicates that the package should be installed globally. You can run this command from anywhere on the file system. npm adds this package to a special directory accessible to all system users. You can find out its location with another npm command:

npm config ls -l

...
prefix = "/Users/tirion/.asdf/installs/nodejs/14.5.0/.npm"
...

After installation, the utility becomes available on the command line by a name specified in the project documentation, e.g., on GitHub. In the case of sloc, the name of the package is the same as the command name:

# Note the period after the sloc command
# It means the current directory needs to be analyzed
sloc .

---------- Result ------------

            Physical :  785627
              Source :  550712
             Comment :  138793
 Single-line comment :  31810
       Block comment :  107078
               Mixed :  6852
 Empty block comment :  2495
               Empty :  105469
               To Do :  385

Number of files read :  6018

----------------------------

asciicast

Recommended programs