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:
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.
The Hexlet support team or other students will answer you.
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.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From zero to a developer. Refunds in case you won't get a job
Sign up or sign in
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.