Skip to content
Tutorials Center
My learning

Topics

Lesson 78 of 156 ยท Modules and Tooling

Packages, npm and Build Tools

Understand the JavaScript toolchain - npm packages, package.json, semantic versions, bundlers, transpilers, linters and formatters - and what each tool does.

Open almost any professional JavaScript project and you'll find more than .js files: a package.json, a node_modules folder with thousands of files, config files for tools with names like Vite, ESLint and Prettier. To a beginner this can look like a wall of mystery. It isn't. Each tool solves one specific problem, and once you know the problems, the tools make sense.

This lesson is a map, not an installation guide. You won't install anything here. Instead you'll learn what npm, bundlers, transpilers, linters and formatters do and why projects use them, and you'll play with tiny, simplified versions of some of them in the editors to see the ideas at work.

Packages and npm

A package is a folder of reusable code with a description file called package.json. Instead of writing your own date formatting or chart drawing from scratch, you install a package someone else has written and tested.

npm is two things that share a name:

  • the registry, a huge public collection of packages at npmjs.com, and
  • the command-line tool that comes with Node.js and downloads packages from it.

The everyday commands look like this:

TEXT
npm init -y                 # create a package.json in the current folder
npm install date-fns        # add a package your app uses at run time
npm install --save-dev vite # add a tool you only need while developing
npm install                 # install everything listed in package.json
npm run build               # run the "build" script from package.json
npx eslint .                # run a package's command without a global install

Installed packages go into node_modules/. That folder is generated and can be enormous, so it's never committed to Git; anyone can recreate it with npm install. Alternatives such as pnpm, Yarn and Bun use the same registry and the same package.json, just with different speed and disk-space trade-offs.

Reading package.json

package.json is plain JSON that describes your project. These fields matter most:

JSON
{
  "name": "recipe-box",
  "version": "1.4.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "lint": "eslint src",
    "test": "vitest"
  },
  "dependencies": {
    "date-fns": "^4.1.0"
  },
  "devDependencies": {
    "eslint": "^9.0.0",
    "vite": "^6.0.0"
  }
}
  • dependencies: packages your code needs when it runs.
  • devDependencies: tools you only need while building and testing. They never ship to users' browsers.
  • scripts: named commands. npm run lint runs eslint src; npm test is a shortcut for the test script.
  • "type": "module": tells Node.js to treat .js files as standard modules with import/export.

Because it's JSON, a program can read it like any other data:

JS Inspecting a package.json

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Semantic versioning and the lock file

Package versions follow semantic versioning (semver): MAJOR.MINOR.PATCH, such as 4.1.0.

  • PATCH goes up for bug fixes (4.1.0 โ†’ 4.1.1).
  • MINOR goes up for new features that don't break existing code (4.1.1 โ†’ 4.2.0).
  • MAJOR goes up for breaking changes (4.2.0 โ†’ 5.0.0). Upgrading may require changes to your code.

The symbols in package.json are ranges that say which future versions are acceptable:

  • ^4.1.0 (caret, the default) allows any 4.x.y at or above 4.1.0, but not 5.0.0.
  • ~4.1.0 (tilde) allows only patches: 4.1.x.
  • 4.1.0 with no symbol allows exactly that version.

For 0.x versions the caret is stricter, because anything below 1.0 is considered unstable: ^0.3.2 only allows 0.3.x. The editor below implements a simplified version of these rules so you can test some ranges:

JS A tiny semver range checker

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Ranges mean two people running npm install a month apart could get different versions. The lock file (package-lock.json, pnpm-lock.yaml and so on) prevents that. It records the exact version of every package installed, including the dependencies of your dependencies. Commit it to Git, so everyone on the team, and your deployment server, installs exactly the same code. Use npm ci on servers for a clean install that follows the lock file strictly.

Every package is code you trust. Installing a package runs other people's code on your machine and ships it to your users. Before adding one, check that it's actively maintained and widely used, and ask whether a few lines of your own would do. Run npm audit to check for known vulnerabilities.

Bundlers

Browsers understand modules natively, so why bundle at all? Because a real app might have hundreds of modules plus packages from node_modules. Loading each one as a separate request, with bare names the browser can't resolve, would be slow or simply wouldn't work. A bundler starts at your entry file, follows every import to build the dependency graph, and writes a few optimized files for production. Along the way it usually:

  • resolves bare imports like "date-fns" to files in node_modules,
  • tree-shakes: drops exports that nothing imports,
  • splits code at each dynamic import() so rarely used parts load later,
  • minifies: removes whitespace and comments and shortens names,
  • adds content hashes to file names (app.3f9a1c.js) so browsers can cache them forever.

Popular choices are Vite (a fast development server plus a production build), esbuild, Rollup, webpack and Parcel. In development, tools like Vite also provide hot module replacement: when you save a file, the page updates in place without a full reload.

Here is the heart of a bundler in miniature: walk the import graph from an entry file, find the order to include modules (dependencies first) and spot a file that nothing uses.

JS A toy bundler's dependency graph

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Real bundlers do the same walk, then go further: they parse every file, track which individual exports are used, and rewrite everything into compact output.

Transpilers

A transpiler (or compiler) rewrites code from one form of JavaScript into another. Common jobs:

  • TypeScript โ†’ JavaScript. TypeScript adds type annotations that catch mistakes before the code runs; the types are stripped out to produce plain JavaScript.
  • JSX โ†’ JavaScript. React's HTML-like syntax becomes ordinary function calls.
  • New syntax โ†’ older syntax. If you must support old browsers, features like ?. and ?? are rewritten into code those browsers understand.

Babel, SWC, esbuild and the TypeScript compiler are the usual tools, and bundlers run them for you. Here's what down-leveling looks like for optional chaining and nullish coalescing. Both versions run, and produce the same output:

JS Modern syntax and its transpiled form

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that Tess's empty string survives in both versions: ?? only replaces null and undefined, and a correct transpiler preserves that meaning exactly. Today all current browsers support modern syntax, so many projects only transpile TypeScript or JSX and leave the rest alone.

Linters and formatters

A linter reads your code without running it and reports likely bugs and risky patterns: variables declared but never used, == where === was probably meant, an await inside a loop that should be parallel, a missing return. ESLint is the standard JavaScript linter; each check is a "rule" you can turn on or off. Most editors show its warnings as you type.

A formatter such as Prettier (or Biome, which does both jobs) rewrites code into one consistent style: indentation, quotes, line breaks, trailing commas. Formatting arguments in code review simply disappear.

Under the hood, real linters parse code into a syntax tree. The toy below just uses regular expressions line by line, but it shows the idea of rules producing warnings:

JS A toy linter with three rules

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A real ESLint setup would also flag unused (declared, never read), because it understands scopes rather than matching text.

Do you need all this?

No, not to start. A single HTML file with a <script type="module"> works perfectly for learning, small sites and prototypes. Reach for tools as the problems they solve appear:

  • You want a library from npm โ†’ npm (plus an import map or a bundler).
  • Your app grows to many files and you care about load time โ†’ a bundler such as Vite.
  • You want type checking or JSX โ†’ TypeScript or a transpiler, usually via the bundler.
  • You work in a team or want fewer silly bugs โ†’ ESLint and Prettier from day one; they're cheap and pay off immediately.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Where should a build tool like Vite be listed in package.json?

    Tools you need only while developing and building belong in devDependencies; they don't ship to users.
  2. 2. A package is listed as ^2.3.1. Which version may npm install?

    The caret allows newer minor and patch versions within the same major version, at or above 2.3.1.
  3. 3. What is the lock file for?

    Ranges allow different versions over time; the lock file pins the exact versions so every install gets the same code.
  4. 4. Which tool combines many modules into a few optimized files and removes unused exports?

    Bundlers follow the import graph, tree-shake unused code, split chunks and minify the output.
  5. 5. What does a linter like ESLint do?

    Linters inspect source code statically. Formatters handle style; test runners run tests.

Summary

  • npm is both a package registry and the command that installs packages into node_modules/.
  • package.json lists dependencies, devDependencies and scripts; the lock file pins exact versions. Commit both, never node_modules.
  • Semver is MAJOR.MINOR.PATCH; ^ accepts compatible updates, ~ only patches.
  • Bundlers follow imports, tree-shake, split and minify code for production; Vite also gives a fast dev server.
  • Transpilers turn TypeScript, JSX or new syntax into JavaScript a target environment can run.
  • Linters catch likely bugs; formatters enforce consistent style. Add tools as their problems appear.

Next: JavaScript on the Server with Node.js.