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
----------------------------
Do it yourself
- Install the sloc utility globally
- Run it in the nodejs-package project to count lines of code
- Run sloc in hexlet-js project, too, and compare the results of the analysis of both projects
- As an experiment, try installing hexo.io, a blogging framework. On the main page of the project, there's some information on how to install and run it
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.