You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
npm (Node Package Manager) is both a command-line tool and an online registry that comes bundled with Node.js. It lets you install, manage, and publish reusable JavaScript packages.
Every Node.js project has a package.json file that describes the project and its dependencies. Create one with:
npm init
Answer the prompts, or use the -y flag to accept all defaults:
npm init -y
A minimal package.json looks like this:
{
"name": "my-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {}
}
Install a package and save it as a dependency:
npm install express
Install a development-only tool (not needed in production):
npm install --save-dev nodemon
Install a package globally (available system-wide as a CLI tool):
npm install -g typescript
| Section | Purpose | Example |
|---|---|---|
| dependencies | Required at runtime | express, lodash |
| devDependencies | Required during development | nodemon, jest, eslint |
When you install packages, npm downloads them into node_modules/. You should add this folder to .gitignore — it can be regenerated at any time from package.json by running:
npm install
The package-lock.json file records exact version numbers of every installed package, ensuring reproducible installs across environments. Commit this file to version control.
The scripts section in package.json lets you define shortcut commands:
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "jest"
}
Run them with npm run:
npm run dev
npm run test
npm start
npm list # list installed packages
npm outdated # show outdated packages
npm update # update packages within semver range
npm uninstall lodash # remove a package
npm audit # check for security vulnerabilities
npm uses semantic versioning (semver): MAJOR.MINOR.PATCH. In package.json:
Understanding npm is essential for every Node.js developer. Nearly every project you work on will rely on third-party packages, and knowing how to manage them confidently keeps your projects healthy and secure.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.