Getting started with NPM: What is a package manager?
🎯 Introduction
In this article, you will learn what NPM is, why it is an essential tool in frontend projects, and how it relates to Node.js. You will also initialize a project and install your first third-party dependency, taking your first step toward a structured development workflow.
🧱 What is NPM?
NPM stands for Node Package Manager. It is a command-line tool installed with Node.js that helps you:
- Install packages written by other developers, such as Vue, React, and Axios.
- Manage project dependencies so everyone installs consistent versions.
- Run scripts, such as
npm run devandnpm run build. - Publish your own packages for others to use.
✨ How NPM relates to Node.js
When first encountering NPM, it is easy to confuse it with Node.js. Their roles are straightforward:
- Node.js is an environment that runs JavaScript on your computer, outside the browser.
- NPM is the package manager bundled with Node.js. Installing Node.js usually installs NPM automatically.
After installing Node.js, check their versions in your terminal:
node -v
npm -v
This course uses Node.js v22.0.0 with NPM 10.x. Run the commands above to check that your environment is ready.
🚀 Initialize your first NPM project
Open a terminal in an empty folder and run:
npm init
NPM asks for details such as the project name, version, and description. Once you answer, it creates a package.json file in the current directory.
To skip the questions and use the defaults, run:
npm init -y
The generated package.json looks something like this:
{
"name": "my-first-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
This file acts as the project's identity card, recording its basic information and dependencies. We will explore it in detail later.
📦 Install your first dependency
Suppose you want to format dates with dayjs. Run:
npm install dayjs
NPM downloads dayjs into the project's node_modules folder and records it under dependencies in package.json.
This course uses dayjs ^1.11.13. Specify the version during installation to match the course environment:
npm install dayjs@1.11.13
After installation, package.json contains:
{
"dependencies": {
"dayjs": "^1.11.13"
}
}
Use it in your code like this:
const dayjs = require('dayjs');
console.log(dayjs().format('YYYY-MM-DD HH:mm:ss'));
^1.11.13 allows the latest compatible version in the 1.x series, without automatically upgrading to 2.0.0. This range allows patch updates while avoiding breaking changes from a new major version.❓ Questions and answers
Q1: Is NPM only for Node.js projects?
No. Projects that use third-party packages from the JavaScript ecosystem, such as Vue, React, or Webpack, use NPM to manage them. It serves both Node.js backend projects and frontend projects.
Q2: What is package.json for?
It is the project configuration file, recording project details, scripts, and dependencies. When you get a project, run npm install to install its dependencies.
Q3: Can I install NPM without Node.js?
NPM requires Node.js. The recommended way to get it is to install Node.js, which includes NPM.
Q4: Where does npm install put packages?
In the current project's node_modules folder, which you normally leave out of version control.
🧪 Practice
Complete the following steps:
- Create an empty folder named
npm-demo. - Run
npm init -yin that folder to generatepackage.json. - Run
npm install dayjs@1.11.13to install dayjs. - Create
index.jsand use dayjs to print the current date and time.
// Write code here to print the current time using dayjs
🎉 You now understand the basics of NPM and project initialization! In the next article, we will explore the fields in package.json, the central configuration file for managing your project.
