npm Basics
Learn to initialize projects and install packages using npm: the world's largest software registry and default package manager for Node.js.
What is npm?
npm stands for Node Package Manager. It is a command-line tool that comes bundled with Node.js and serves two major purposes:
Why npm Matters
Modern JavaScript applications rely on dozens or even hundreds of external packages. npm solves the problems that arise when projects depend on shared code:
The npm Ecosystem
The package.json File
Every npm project has a package.json file at its root. This is the project's manifest: a JSON file that records metadata, dependencies, and scripts.
{
"name": "my-app",
"version": "1.0.0",
"description": "A sample project",
"main": "index.js",
"scripts": {
"test": "echo \"Error\" && exit 1"
},
"dependencies": {}
}
node_modules & the Lock File
dependencies vs devDependencies
| dependencies | devDependencies | |
|---|---|---|
| Purpose | Needed at runtime | Needed only during development |
| Example | express, react | jest, eslint |
| Install flag | npm install <pkg> | npm install --save-dev <pkg> |
| Shipped to users? | Yes | No |
The npm Workflow
Example 1: Initializing a Project
Let's create a brand-new project called weather-app:
$ cd weather-app
$ npm init -y
After running this command, npm creates a package.json file in your project folder. Your project is now an official npm project, ready for packages.
Example 2: Installing a Package
Now install axios, a popular HTTP request library:
Three things happen automatically:
Example 3: Using the Package
Create an index.js file and use the installed package:
const axios = require('axios');
axios.get('https://api.weather.gov')
.then(res => {
console.log(res.data.status);
})
.catch(err => {
console.error(err.message);
});
npm Scripts
The "scripts" section in package.json lets you define custom terminal commands:
"start": "node index.js",
"test": "jest",
"build": "webpack --mode production"
}
Global vs Local Install
| Global (-g) | Local (default) | |
|---|---|---|
| Command | npm install -g <pkg> | npm install <pkg> |
| Installed to | System-wide bin folder | Project's node_modules/ |
| Tracked in package.json? | No | Yes |
| Best for | CLI tools (e.g. nodemon) | Project dependencies |
Semantic Versioning
npm uses semver (Semantic Versioning) to track package versions. A version like 1.4.2 has three parts:
Common Mistakes
npm Commands Cheat Sheet
| Command | What It Does |
|---|---|
| npm init -y | Creates package.json with defaults |
| npm install <pkg> | Installs a package as a dependency |
| npm install --save-dev <pkg> | Installs a package as a dev dependency |
| npm install | Installs all dependencies from package.json |
| npm uninstall <pkg> | Removes a package and updates package.json |
| npm run <script> | Runs a custom script from package.json |
Question 1 of 5
Which command creates a package.json file in a new project?
Question 2 of 5
After running npm install axios, where is the axios source code stored?
Question 3 of 5
You want to install eslint only for development, not for production. Which command is correct?
Question 4 of 5
In the version "^2.3.1", what does the ^ (caret) allow npm to update?
Question 5 of 5
A teammate clones your repository. Which single command installs all the project's dependencies?