Language: EN

como-inicializar-un-proyecto-npm

How to initialize a project with NPM

The first step to working with a project in NPM is to initialize the project. Basically, this process will create the package.json file that contains information about the project, its dependencies, custom scripts, among other important aspects.

But there’s no need to create the file manually. In fact, it’s not advisable. Instead, NPM itself will create this file for us. This is what we call initializing the project. Let’s see how it’s done.

First of all, we usually want to have a folder for our project. We can do this through the file explorer, or using the command line by doing

mkdir my-project
cd my-project

Now, to initialize the project we simply have to run

npm init

This command launches an interactive wizard that asks us (a really, really long list!) of 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 things (which is what I usually do).

Once this process is finished, a package.json file will be created in the project directory, which will contain all the information provided during initialization.

If you have accepted all the default options, it will look 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"
}

Subsequently, 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": "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 the name, version, description, author, license, among others.

Also, we will also find a section called dependencies, where we can specify the dependencies that our project will use. But we’ll see that in the next article.