npm and packages

Topic 2 of 2 in Modules & packages6 min readBeginnerFree course

Overview

npm is the package manager that comes with Node.js. It downloads libraries other developers have published, so you don’t have to write everything yourself. The npm registry holds millions of packages: date libraries, chart libraries, testing tools, whole frameworks like React.

You’ll use npm even for browser projects, because modern front-end tools (bundlers, formatters, linters, frameworks) are themselves npm packages.

To use it, install Node.js (the LTS version) from nodejs.org. npm comes with it. Check both are there:

Terminal
node --version
npm --version

Starting a project

Terminal
npm init -y

This creates package.json, which describes your project and its dependencies. The -y accepts the default answers; you can edit the file afterwards.

JSON
{
  "name": "my-project",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "start": "node index.js"
  }
}

Add "type": "module" so Node treats your .js files as ES modules and import/export work.

Installing packages

Terminal
npm install date-fns        # a dependency your app uses
npm install -D prettier     # a dev dependency: tools for building or testing

npm install date-fns downloads the package from the npm registry into node_modules, adds “date-fns”: “^4.1.0” to package.json and records exact versions in package-lock.json. Below, a version number 4.1.0 is labelled MAJOR (breaking changes), MINOR (new features) and PATCH (bug fixes); ^4.1.0 allows any newer 4.x.x.

JSON
{
  "dependencies": { "date-fns": "^4.1.0" },
  "devDependencies": { "prettier": "^3.3.0" }
}

The code itself goes into the node_modules folder. Don’t commit that folder — anyone can recreate it with npm install. The package-lock.json file records exact versions so everyone gets the same ones.

Then import the package by its name:

JavaScript
import { format, addDays } from 'date-fns';
format(addDays(new Date(), 7), 'd MMM yyyy'); // a week from today

Common commands

Command What it does
npm install install everything listed in package.json (run after cloning a project)
npm install <pkg> add a dependency
npm install -D <pkg> add a dev dependency
npm uninstall <pkg> remove one
npm update update within the allowed version ranges
npm outdated list packages with newer versions
npm ci clean install exactly from the lock file (used on build servers)

dependencies or devDependencies?

  • dependencies — code your app needs while running: a date library, a charting library, React.
  • devDependencies — tools used only while developing: Prettier, ESLint, a test runner, a bundler.

Version ranges

Package versions follow semantic versioning: MAJOR.MINOR.PATCH.

  • PATCH (4.1.1) — bug fixes only.
  • MINOR (4.2.0) — new features, nothing removed.
  • MAJOR (5.0.0) — breaking changes; your code may need updating.

^4.1.0 means “4.1.0 or any newer 4.x version”. A change in the first number (5.0.0) may break things, so npm won’t pick it automatically. Upgrading a major version is a deliberate step: read the package’s changelog, then npm install date-fns@5.

Scripts

JSON
{
  "scripts": {
    "dev": "astro dev",
    "build": "astro build",
    "format": "prettier --write ."
  }
}

Run them with npm run dev, npm run build and so on (npm start and npm test work without run). Scripts give a project one agreed set of commands — a new teammate doesn’t need to know which tools you use, only npm run dev.

Tools installed in the project are available inside scripts by name, without a path.

npx

npx runs a package’s command without installing it permanently — handy for one-off tools and project generators:

Terminal
npx prettier --check .
npx create-vite@latest my-app

Using packages in the browser

Browsers can’t read node_modules directly or understand bare names like 'date-fns'. A build tool solves this: it follows your imports, pulls in the package code, and bundles everything into files the browser can load. Vite is a popular, fast choice:

Terminal
npm create vite@latest my-app
cd my-app
npm install
npm run dev

For a quick experiment without a build step, you can import from a CDN instead: import { format } from 'https://esm.sh/date-fns';.

Check before you install. Look at a package’s downloads, recent updates and size. Every dependency is code you’re trusting.

A few good habits:

  • Prefer well-maintained packages with many users.
  • Don’t install a package for something a few lines of code can do.
  • Run npm audit to check for known security problems.
  • Watch for typos in names — fake packages sometimes imitate popular ones.
Exercise

Set up a project

In an empty folder, create a project with npm init -y, install date-fns, and add a start script that runs node index.js. Write index.js so it prints today’s date formatted as d MMMM yyyy, then run npm start.

JavaScript
// index.js
// import format from date-fns and print today's date

Summary

  • npm installs packages into node_modules and records them in package.json.
  • dependencies ship with your app; devDependencies are only for building and testing.
  • Commit package.json and package-lock.json, never node_modules.
  • Versions follow MAJOR.MINOR.PATCH; ^ allows new minor and patch versions.
  • Scripts in package.json give your project short, shared commands; npx runs tools without installing them.