The first step to work with a project in NPM is to initialize the project *(logical). Basically, this process will create the package.json
file that contains information about the project, its dependencies, custom scripts, among other important aspects.
But we don’t need to create the file by hand. In fact, it’s not advisable at all. Instead, NPM itself will create this file for us. This is what we call initializing the project. Let’s see how it would be done.
First of all, we will usually want to have a folder for our project. We can do this through the file explorer or by using the command line with:
mkdir my-project
cd my-project
Now, to initialize the project, we simply have to run:
npm init
This command launches an interactive assistant that will ask us (a very long list!) initial options for our project.
package name: (my-project)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
We can enter our own values as needed, or just hit ‘Enter’ like crazy to accept the default parameters until it finishes asking questions (which is what I usually do).
Once this process is completed, a package.json
file will be created in the project directory, which will contain all the information provided during the initialization.
If you accepted all the default options, it will look something like this.
{
"name": "my-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Later, we can modify these values in the package.json
file at any time, depending on our needs. To do this, we just need to open it with our favorite text editor.
For example, a simple package.json
file for this example could look like this:
{
"name": "my-project",
"version": "1.0.0",
"description": "An example project",
"author": "Your name",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "MIT",
"dependencies": {
"express": "^4.17.1"
}
}
Within this file, we will find different sections where we can define relevant information for our project, such as name, version, description, author, license, among others.
Additionally, we will also find a section called dependencies
, where we can specify the dependencies that our project will use. But we will see that in the next article.