Initialize a project in node with npm init

Aunsh Bandivadekar
5 min readFeb 25, 2022

--

Photo by Alexandre Debiève on Unsplash

This article is the second part of the “Creating a server using node and express” series.

Check out other articles in this series:

  1. Part 1: Installing the required tools and software(Node, VS Code, Postman, Git).
  2. Part 2: Initialize project with npm init in node
  3. Part 3: Creating a server using node and express

Intro:

In the previous article, we downloaded and installed the tools required to build our server. Now, we will initialize our project by creating a package.json file.

Requirements: Level — beginner

  1. Internet connection
  2. Javascript basics
  3. Computer

What we will be doing?

  1. Initializing the project (creating folder, npm init)
  2. Understand the data fields in package.json

Initializing the project:

To get started with building our server first we need to create a folder.

Steps:

  1. Open VS Code

2. Open ‘File’ (top left corner) and select Open folder (CTRL + K)

3. Once the “Open Folder” prompt is open right-click to create a new folder and press “Select Folder”. I have created a new folder called “node-server”.

4. Once the folder is opened you’ll see a prompt asking you whether you trust the authors. Accept it.

5. Once that is done you’ll see the folder you just created on the top left panel. Viola! your folder is open. Now we need to initialize our project with the help of the npm init command.

6. Press CTRL + ` ( ⌘ + ` for Mac) to toggle the terminal. This will open the ‘Windows Powershell’ (windows).

7. For our project we will be using the git bash terminal which we installed in the previous article. Click the ‘+’ icon on the top right corner of the terminal and select git bash.

In the image above git bash is the default terminal for me which may or may not be the case for you. But that’s fine. After selecting the option, this is what your terminal will look like. Note the change in colour of the text which is now yellow.

8. Initializing the project with npm init -y

What is this command?

npm init -y

  • npm

npm (node package manager) is the world’s largest Software Registry. Open-source developers use npm to share software. It has over 1M packages.

A package is a piece of code that is made for dealing with specific functionality.

  • init

init stands for initialization. Initialization creates a package.json file that includes information (metadata) about our project. This metadata in its most basic form consists of the following fields:

  1. name: Name of the project (Here it is ‘node-server’).
  2. version: As we just initialized the version is 1.0.0. If there are new releases then the version will increase (eg: 1.1.0, 1.2.0 etc).
  3. description: Add a short description of what the project is about.
  4. main: This is the module ID that is the primary entry point to your program.
  5. scripts: A string of commands entered in the command line to run the application or do a specific task.
  6. keywords: Collection of words used to identify the software, package or modules.
  7. author: People who have created the software, package or modules.
  8. license: The agreement which binds the users of the package to work within specific rules. Here it is ‘ISC’.
  • -y

The -y flag (short for yes) is used to automatically answer “yes” to any prompts that npm might print on the command line. To get the project started faster we will use the -y flag.

In the command terminal type npm init -y and press enter.

After the project is initialized you will see the “package.json” file which will have the fields discussed above.

9. What is package.json ?

The package.json file records all of the important metadata of a project. It includes the dependent packages your code needs to work, author, name, scripts, proxies and much more. It is the soul of any npm package.

Opening the package.json file in your project folder will look something like the following:

With this, your project is initialized and now we can start working on building the server.

Conclusion:

In this article, we saw how to initialize a project with the npm init command and the use of the “package.json” file which is crucial in order to start coding our projects in node.

Next Up:

Part 3 in the series “Creating a server using node and express”

Creating a server using node and express

--

--

Aunsh Bandivadekar
Aunsh Bandivadekar

Written by Aunsh Bandivadekar

Graduate Student @ UC Davis | Engineer | Teacher | Developer | Writer | Check out my portfolio @ aunsh.com

No responses yet