The first step to working with an NPM project is to initialize the project (logical). Basically, this process will create the package.json file which contains information about the project, its dependencies, custom scripts, among other important aspects.
But we don’t need to create the file manually. In fact, it’s not advisable at all. Instead, it is NPM itself that will create this file for us. This is what we call initializing the project. Let’s see how it’s done.
First, we usually want to have a folder for our project. We can do this through the file explorer, or via the command line by doing
mkdir mi-proyecto cd mi-proyecto
Now, to initialize the project we simply have to execute
npm init
This command launches an interactive assistant that asks us (a very long list!) of initial options for our project.
package name: (mi-proyecto) 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 simply press ‘Enter’ like crazy to accept the default parameters, until it finishes asking things (which is what I normally do).
Once this process is finished, a package.json file will be created for us 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”: “mi-proyecto”, “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, according to our needs. To do this, we simply have to open it with our favorite text editor.
For example, a simple package.json file for this example could look like this:
{ “name”: “mi-proyecto”, “version”: “1.0.0”, “description”: “Un proyecto de ejemplo”, “author”: “Tu nombre”, “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.
Furthermore, we will also find a section called dependencies, where we can specify the dependencies our project will use. But we’ll see that in the next article.
