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:
npm init -yThe -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.
npm install chalkThree things happen:
- A
node_modulesfolder appears, containing the package's code (and anything it needs). - A
package-lock.jsonfile appears, recording the exact versions installed so everyone gets the same setup. - Your
package.jsongains a"dependencies"entry.
Use it in a script:
// 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
| Command | What 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:
{
"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_modulesby hand. It's regenerable. Delete it and runnpm installand it comes back. In fact, projects usually exclude it from backups and version control. - Always commit (or back up)
package.jsonandpackage-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?