Initializing an NPM Project
Initializing an NPM project is the first step when creating a Node.js application. NPM helps us manage project information, install packages, and execute commands.
In this lesson, we will learn how to create a project folder, initialize
NPM, understand the generated package.json file, install
packages, and run a simple Node.js application.
1. What Does Initializing a Project Mean?
Initializing a project means preparing a folder to work as a Node.js
project. During initialization, NPM creates a
package.json file that stores project details and
configuration.
For example, suppose we want to create a student management application.
npm init
2. Prerequisites
Before initializing a project, Node.js and NPM should be installed on your computer. NPM is normally included with Node.js.
Open a terminal or command prompt and execute these commands:
node -v
npm -v
Example output:
v22.x.x
10.x.x
The exact version numbers may differ depending on the installed Node.js and NPM versions.
3. Create a Project Folder
First, create a folder for your application. We can create the folder using File Explorer or directly from the terminal.
mkdir StudentManagement
cd StudentManagement
Explanation:
| Command | Purpose |
|---|---|
mkdir StudentManagement |
Creates a new project folder. |
cd StudentManagement |
Moves into the project folder. |
4. Initialize NPM Using npm init
The npm init command starts an interactive process.
NPM asks questions about the project, such as its name, version,
description, entry point, and license.
npm init
Example of the information NPM may request:
package name: (studentmanagement) student-management
version: (1.0.0) 1.0.0
description: Student Management Application
entry point: (index.js) app.js
test command:
git repository:
keywords:
author:
license: (ISC) ISC
After you confirm the information, NPM creates the
package.json file.
Using npm init -y
If you do not want to answer each question, use
npm init -y. It creates a
package.json file using default values.
npm init -y
npm init asks questions.
npm init -y accepts the default values automatically.
5. Understand the package.json File
The package.json file contains project metadata,
dependencies, and scripts. It helps NPM understand how your
application is configured.
{
"name": "student-management",
"version": "1.0.0",
"description": "Student Management Application",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"author": "",
"license": "ISC"
}
| Property | Meaning |
|---|---|
name |
The project name. |
version |
The current project version. |
description |
A short explanation of the project. |
main |
The package entry-point field; its meaning depends on how the package is used. |
scripts |
Defines commands that can be executed using NPM. |
dependencies |
Lists packages required at runtime. |
devDependencies |
Lists packages used during development. |
6. Create Your First Node.js File
After initializing NPM, create an app.js file inside
the project folder.
console.log("Welcome to Student Management!");
const studentName = "Rahul";
const course = "Node.js";
console.log("Student Name:", studentName);
console.log("Course:", course);
Output:
Welcome to Student Management!
Student Name: Rahul
Course: Node.js
The console.log() function displays messages in the
terminal. This example confirms that your Node.js file can execute.
7. Run the Node.js Application
You can run the application directly by using the Node.js command.
node app.js
You can also add a start script to your
package.json file:
{
"scripts": {
"start": "node app.js"
}
}
Now run the application using:
npm start
Output:
Welcome to Student Management!
Student Name: Rahul
Course: Node.js
scripts object, make sure you retain any other
scripts your project needs. The JSON file must also remain valid.
8. Install a Package
NPM allows us to install reusable packages instead of writing every feature from scratch. For example, Express is commonly used to build web applications and APIs with Node.js.
npm install express
After a successful installation, the project may contain:
node_modules contains installed packages,
package.json records project configuration and
dependency declarations, and package-lock.json
records the resolved dependency tree for reproducible installs.
9. Common NPM Commands
| Command | Purpose |
|---|---|
npm init |
Initializes a project interactively. |
npm init -y |
Initializes a project with default values. |
npm install |
Installs the dependencies declared by the project. |
npm install express |
Installs Express and records it as a dependency. |
npm start |
Runs the configured start script, if present. |
npm list |
Displays the installed dependency tree. |
10. Practice Task
Task: Create a Book Management project.
- Create a folder named
BookManagement. - Open the folder in your terminal.
- Run
npm init -y. - Create an
app.jsfile. - Print the book name, author, and price using
console.log(). - Add a
startscript inpackage.json. - Run the program using
npm start.
Expected result: The terminal should display the book details entered in your program.
Summary
Initializing an NPM project prepares a folder for Node.js
development. The npm init command creates a
package.json file interactively, while
npm init -y creates it using default values.
The package.json file stores project information, scripts,
and dependencies.
We can create an app.js file, execute it using
node app.js, configure an NPM start script,
and install packages using npm install.
These are the basic steps for setting up a Node.js project.