JavaScript Modules & Ecosystem

Modules, npm & the Ecosystem

JavaScript's real power is its package ecosystem. This module covers how code is organized, shared, installed, and bundled for production.

Tooling 5 Topics
01

ES Modules — import / export

Before ES2015, JavaScript had no built-in module system — every script shared one global scope. ES Modules give each file its own scope, with explicit exports and imports.

JavaScript — math.js
// math.js
export function square(n) { return n * n; }
export const PI = 3.14159;
export default function add(a, b) { return a + b; }  // one default export per file

// app.js
import add, { square, PI } from "./math.js";
import * as math from "./math.js";   // grab everything as a namespace
In the browser, load an ES module with <script type="module" src="app.js"> — modules are deferred and strict-mode by default.
02

npm & package.json

npm is JavaScript's package manager and registry, the world's largest — over 3 million packages. package.json declares what your project needs and how to run it.

JavaScript — package.json
{
  "name": "my-app",
  "version": "1.0.0",
  "type": "module",
  "scripts": { "start": "node index.js", "test": "vitest" },
  "dependencies": { "express": "^4.19.0" },
  "devDependencies": { "vitest": "^2.0.0" }
}

# npm install express      — adds to dependencies, downloads into node_modules/
# npm install -D vitest    — adds to devDependencies (dev/test tools only)
# npm run start            — runs the "start" script
package-lock.json pins the exact resolved versions of every dependency, direct and transitive. Commit it — without it, two installs of the same package.json can silently pull different sub-versions.
03

CommonJS vs ES Modules

Node.js predates ES Modules and used its own system, CommonJS, for years. Both are still common in the wild, and mixing them incorrectly is a frequent source of "unexpected token" errors.

CommonJSES Modules
Exportmodule.exports = xexport default x
Importconst x = require("x")import x from "x"
LoadingSynchronousSupports async, statically analyzable
File signal.cjs, or default in Node.mjs, or "type": "module"
04

Bundlers & Transpilers

Browsers can't efficiently load thousands of separate module files, and not every browser supports every new syntax feature. Bundlers and transpilers solve those two problems.

ToolWhat it does
ViteFast dev server + bundler, the modern default for new projects
webpackOlder, highly configurable bundler — still common in large existing apps
esbuildExtremely fast bundler/minifier written in Go, often used under other tools
BabelTranspiles modern JS syntax down to a version older browsers understand
TypeScriptAdds static types on top of JS, compiles (transpiles) back down to plain JS
05

Popular Libraries

A handful of packages show up in almost every real-world JavaScript codebase.

Express
Minimal backend web framework for Node — routes, middleware, REST APIs.
React
UI library built around components and a virtual DOM — the most-used frontend tool.
Axios
HTTP client with a friendlier API than raw fetch — request/response interceptors.
Lodash
Utility functions for arrays, objects, and strings that predate many native equivalents.
Zod
Runtime schema validation with TypeScript type inference built in.
Jest / Vitest
Test runners for unit and integration tests, with built-in assertions and mocking.

Quick Quiz

1. How many default exports can one ES module file have?
2. package-lock.json exists to…
3. CommonJS uses which syntax to import?
4. A bundler's main job is to…
5. npm install -D vitest adds vitest to…