Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript3 tutorials
  • Node.js Introduction2 tutorials
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth
  • JavascriptJavascript · Advanced · Mid
  • JavascriptJavascript · Advanced · Short

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 9

Managing Dependencies with npm

So far, your programs have only used code you wrote yourself, plus tools that come built into Node.js. Real projects almost always borrow code that other people wrote. Maybe you need to read dates in many formats, talk to a database, or build a web server with routing. Writing all of that yourself w
Level: GeneralLength: Mid2 min read

Read at another depth

Managing Dependencies with npm

What npm Is

So far, your programs have only used code you wrote yourself, plus tools that come built into Node.js. Real projects almost always borrow code that other people wrote. Maybe you need to read dates in many formats, talk to a database, or build a web server with routing. Writing all of that yourself would take weeks. npm lets you use ready-made packages instead.

npm is two things at once:

  • The registry: a giant online catalog of free, reusable code packages. Anyone can publish there, and anyone can download.
  • A tool: a command-line program, installed automatically when you install Node.js, that downloads packages and keeps track of them.

A package (also called a dependency, because your project depends on it) is a bundle of JavaScript code with a name and a version number.

The package.json File

Every Node.js project has a file called package.json. Think of it as the project's ID card. It records the project's name, the packages it depends on, and the exact versions. With this file, anyone can rebuild your project's setup with one command.

Create it in your project folder:

Code example in bash
npm init -y

The -y means "use the defaults, don't ask me questions." You can open the file and edit it any time; it's plain text.

Installing a Package

Let's install a popular package called chalk, which adds colors to terminal output.

Code example in bash
npm install chalk

Three things happen:

  1. A node_modules folder appears, containing the package's code (and anything it needs).
  2. A package-lock.json file appears, recording the exact versions installed so everyone gets the same setup.
  3. Your package.json gains a "dependencies" entry.

Use it in a script:

Code example in javascript
// colors.js
import chalk from 'chalk';

console.log(chalk.green('Success!'));
console.log(chalk.red.bold('Something went wrong.'));

> This script uses modern import syntax, which needs a small change. In your package.json, add the line "type": "module", inside the top curly braces. Then run node colors.js.

Useful npm Commands

CommandWhat it does
`npm install`Installs everything listed in package.json
`npm install chalk`Adds a new package and saves it
`npm uninstall chalk`Removes a package
`npm update`Updates packages to newer allowed versions
`npm run <name>`Runs a script defined in package.json

The npm run command is worth a closer look. You can define shortcuts in package.json:

Code example in json
{
  "scripts": {
    "start": "node app.js"
  }
}

Now npm start runs your app. Teams use this to standardize commands like testing and launching.

Keeping Projects Tidy

Two rules save you from common headaches:

  • Never edit node_modules by hand. It's regenerable. Delete it and run npm install and it comes back. In fact, projects usually exclude it from backups and version control.
  • Always commit (or back up) package.json and package-lock.json. Those two small files are enough to reproduce your whole setup.

Finding Packages

Search at npmjs.com or run npm search <word>. When you find one, its page shows installation instructions, documentation, and the version history. For learning, stick to well-known packages with millions of downloads.

Exercise

A teammate clones your project. Which files do they need, and what single command sets them up?

Show answer

They need your source code plus package.json and package-lock.json (not node_modules). Running npm install downloads everything listed and reproduces your setup.

Exercise

What's the difference between npm install chalk and npm install with no package name?

Show answer

With a package name, it downloads that new package and adds it to package.json. With no name, it installs all packages already listed in package.json.

Spotted a mistake, or code that does not work?

Report an issue

Chapter: Managing Dependencies with npm

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that