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

NPM JS: Setting up environment

Installing Node.js adds not only an interpreter but also several important utilities for working with JavaScript projects. Among them is the npm (node package manager) utility. It allows to work with any Javascript project as an npm package.

# How to check if it's available
npm --version

6.14.5

npm performs many important tasks, which we'll get to know during the course. Currently, we're interested in its basic use – the creation (initialization) of a new project. A project is defined as code for an application, such as a website.

To create a new project, you need to do two things:

  1. Create a project directory – a place where all files with code will be located. This directory is called the project root.
  2. Run the npm init command in the project root.
# Creating a directory
mkdir hexlet-hello-world
# Navigating inside the directory
cd hexlet-hello-world

# Project initialization
# init means initialization
npm init

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

Press ^C at any time to quit.
package name: (hexlet-hello-world)

During the initialization process, npm will ask a few questions about what the project should be called, what version it should be, its description, etc. All these questions you can safely skip, you'll always have the chance to change the answers.

In the end, npm will ask if everything is correct and show the contents of the package.json file, which will be the basis of the new project. If your answer is yes, the file will be written to disk and the project will be considered ready for work.

{
  "name": "tmp",
  "version": "1.0.0",
  "type": "module",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

package.json is a text file that stores data in JSON format. This format allows you to describe data as key-value pairs, where the value can be JSON itself. In the example above, it's the value of the scripts key.

Note the "type" key in JSON above. You need to add this part yourself by editing the file. It's needed in order for the import system to work.

After the initialization process is complete, you can get down to the most important thing, writing code. By default, the code is created inside the index.js file in the project root (where the package.json file has been created). This does not mean that the entire package consists of a single file. You can create as many code files as you want, but the main thing is that most of the work is done inside index.js, where the code is imported from the other files.


Do it yourself

  1. In the hexlet-js project, initialize the JavaScript project. Don't forget the type key above.
  2. Fill in the blank fields in the package.json file, see the npm documentation for a detailed description of each field.
  3. Run the code in index.js from the previous lesson, make sure everything works.
  4. Push all the changes to GitHub.

Recommended materials

  1. NPM
  2. Node.js reference repository

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

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.