Initialize a project in node with npm init
This article is the second part of the “Creating a server using node and express” series.
Check out other articles in this series:
- Part 1: Installing the required tools and software(Node, VS Code, Postman, Git).
- Part 2: Initialize project with npm init in node
- 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
- Internet connection
- Javascript basics
- Computer
What we will be doing?
- Initializing the project (creating folder, npm init)
- 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:
- 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:
- name: Name of the project (Here it is ‘node-server’).
- 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).
- description: Add a short description of what the project is about.
- main: This is the module ID that is the primary entry point to your program.
- scripts: A string of commands entered in the command line to run the application or do a specific task.
- keywords: Collection of words used to identify the software, package or modules.
- author: People who have created the software, package or modules.
- 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”