Sub topic 14
Packages, npm, and Bundlers
This chapter assumes you can run scripts with node, that you know the difference between ES modules and CommonJS, and that you are comfortable working in a terminal. It covers how npm manages third-party code and why browser code usually goes through a bundler before it ships.
What npm Does
A package is a folder of code with a package.json file that describes it. npm is the package manager that comes with Node.js. It downloads packages from the npm registry, puts them in a node_modules folder, and records what you installed.
Start a project and add a dependency:
mkdir dates-demo
cd dates-demo
npm init -y
npm install date-fns
npm install --save-dev esbuildAfter these commands, package.json looks roughly like this:
{
"name": "dates-demo",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "node src/main.js",
"build": "esbuild src/main.js --bundle --format=esm --minify --outfile=dist/bundle.js"
},
"dependencies": {
"date-fns": "^4.1.0"
},
"devDependencies": {
"esbuild": "^0.24.0"
}
}You add the "type": "module" line and the scripts entries by hand. The type field makes Node.js treat .js files as ES modules. dependencies lists what your code needs at runtime. devDependencies lists tools that you only need while developing or building. npm run build runs a script, and npm start is a shortcut for the start script.
Version Ranges and the Lock File
Dependency versions follow semantic versioning (semver), written as MAJOR.MINOR.PATCH. A range sets which updates npm may install:
| Range | Allows | Example matches |
|---|---|---|
| `^4.1.0` | Any 4.x version at or above 4.1.0 | 4.1.0, 4.9.2 |
| `~4.1.0` | Patch updates only | 4.1.0, 4.1.7 |
| `4.1.0` | Exactly this version | 4.1.0 |
package-lock.json records the exact version of every installed package, including the dependencies of your dependencies. Commit this file to version control. In CI, use npm ci: it installs exactly what the lock file says, and it fails if the lock file and package.json disagree.
Using a Package in Node.js
Create src/main.js:
import { format } from 'date-fns';
console.log(`Today is ${format(new Date(), 'yyyy-MM-dd')}`);'date-fns' is a bare specifier, meaning it is not a relative path. Node.js resolves it by walking up from the importing file to find a node_modules/date-fns folder. It then reads that package's package.json (the exports or main field) to choose the entry file. Run npm start and the script works.
Why the Browser Needs a Bundler
If you load the same file in a page with <script type="module">, it fails. Browsers do not search node_modules. A bare specifier is an error unless an import map tells the browser which URL it stands for. Unbundled code causes other problems too:
- Each module is a separate HTTP request, and a package like date-fns can pull in hundreds of them.
- Some packages are written in CommonJS, which browsers cannot run.
- Source code ships with comments, whitespace, and functions you never call.
A bundler such as esbuild, Rollup, Vite, or webpack solves these problems at build time. It starts from your entry file and follows every import, resolving bare specifiers the same way Node.js does. It converts CommonJS where needed and writes one file, or a few. Tree shaking drops the exports nobody imports, so only format and its helpers from date-fns reach the bundle. Minification shrinks what is left.
Run npm run build, then reference the output file in your page:
<script type="module" src="dist/bundle.js"></script>Exercise
A teammate's browser page imports import { debounce } from 'lodash-es'; directly and the console shows "Failed to resolve module specifier". The same import works under Node.js. Explain why, and give two ways to fix it.
Show answer
Node.js resolves bare specifiers by searching node_modules, but browsers do not. The browser needs a URL or a mapping. One fix is to run a bundler such as esbuild with --bundle, which resolves the import and inlines lodash-es into the output file. The other is to add an import map that maps "lodash-es" to a URL where the browser can fetch the module.
Exercise
Your package.json lists "date-fns": "^4.1.0". Version 4.3.0 and version 5.0.0 are both published. Which one does a fresh npm install pick when there is no lock file, and what does npm ci do when there is one?
Show answer
npm install picks 4.3.0, because ^4.1.0 allows any 4.x version at or above 4.1.0 and excludes 5.0.0. npm ci ignores the range and installs the exact version recorded in package-lock.json.