Register to get access to 15+ free programming courses with interactive exercises

Global package installation JS: Setting up environment

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

  1. Install the sloc utility globally
  2. Run it in the nodejs-package project to count lines of code
  3. Run sloc in hexlet-js project, too, and compare the results of the analysis of both projects
  4. 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

Аватары экспертов Хекслета

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

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.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending the form, you agree to Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs

From zero to a developer. Refunds in case you won't get a job

Frontend Developer icon
Profession
New
Development of front-end components for web applications
start anytime 10 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending the form, you agree to Personal Policy and Service Conditions

Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.