01 / 21

npm Basics

Learn to initialize projects and install packages using npm: the world's largest software registry and default package manager for Node.js.

JavaScript

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:

Registry
A massive online database of over 2 million open-source JavaScript packages anyone can publish or download.
CLI Tool
A command-line interface that lets you install, update, and manage those packages inside your own projects.

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:

1
Saves time: Reuse code written and tested by thousands of other developers instead of reinventing the wheel.
2
Manages versions: Track exactly which version of each package your project uses, avoiding "works on my machine" bugs.
3
Ensures reproducibility: Anyone who clones your project can run one command and get the exact same dependencies.

The npm Ecosystem

Your Project npm Registry package.json node_modules/ npm CLI downloads from describes installs into managed by

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.

// Minimal package.json
{
  "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

node_modules/
A folder npm creates in your project. It contains the actual source code of every installed package (and their sub-dependencies). This folder can grow very large.
package-lock.json
An auto-generated file that records the exact version of every package installed. This guarantees that every teammate gets the same dependency tree.

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

1
Create a folder for your project and open a terminal inside it.
2
Run npm init (or npm init -y for defaults) to generate package.json.
3
Install packages with npm install <name>. npm downloads them into node_modules/ and updates package.json.
4
Import and use the package in your code with require() or import.
5
Share your project by committing package.json and package-lock.json. Others run npm install to reproduce your setup.

Example 1: Initializing a Project

Let's create a brand-new project called weather-app:

$ mkdir 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:

$ npm install axios

Three things happen automatically:

1
node_modules/ folder is created (if it didn't exist) and axios source code is downloaded into it.
2
package.json is updated: "dependencies": { "axios": "^1.6.0" } is added.
3
package-lock.json is created (or updated) to lock the exact version tree.

Example 3: Using the Package

Create an index.js file and use the installed package:

// index.js
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:

"scripts": {
  "start": "node index.js",
  "test": "jest",
  "build": "webpack --mode production"
}
npm start
Runs the "start" script. Built-in shortcut: no "run" needed.
npm test
Runs the "test" script. Also a built-in shortcut.
npm run build
Custom scripts require the run keyword before the name.

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:

1 4 2 . . MAJOR Breaking changes MINOR New features PATCH Bug fixes

Common Mistakes

✕ Committing node_modules
This folder can contain thousands of files. Commit package.json and package-lock.json instead: others run npm install to recreate it.
✕ Ignoring package-lock.json
Deleting or not committing the lock file means teammates may get different versions, causing hard-to-debug inconsistencies.
✕ Installing everything globally
Global packages aren't tracked in package.json. Other developers won't know which packages they need. Use local installs by default.

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?