Sub topic 7
Why you need a build tool
This chapter assumes you know JavaScript fundamentals, including ES modules (import and export), and basic HTML. You should also have met JSX and components in the earlier chapters. Here you will see why a React project cannot run directly in the browser, and what a build tool does about that.
The gap between what you write and what the browser runs
Browsers understand HTML, CSS and JavaScript. A React project is usually written in a different form:
- JSX, which is not valid JavaScript. A browser that sees
<button onClick={...}>inside a.jsxfile reports a syntax error. - Many small modules spread across dozens or hundreds of files.
- Packages from npm, such as
reactandreact-dom, imported by name. - Assets such as CSS files, SVG images and fonts that you import from JavaScript.
A build tool is a program that reads this source code and turns it into something the browser can load. It also serves your project during development and produces an optimized version for deployment.
Problem 1: JSX must be transformed
JSX is syntax sugar. A compiler turns each JSX element into a function call. With React's modern JSX transform, this source:
function Greeting({ name }) {
return <h1 className="title">Hello, {name}</h1>
}becomes roughly this plain JavaScript:
import { jsx as _jsx } from 'react/jsx-runtime'
function Greeting({ name }) {
return _jsx('h1', { className: 'title', children: ['Hello, ', name] })
}The browser can run the second version. The step that converts the first into the second is called transpiling (or compiling). The build tool runs it for every .jsx file.
Problem 2: bare module imports
Look at the first line of your entry file:
import { createRoot } from 'react-dom/client''react-dom/client' is a bare specifier. It is a package name, not a file path. A browser has no node_modules folder to look in, so it cannot resolve this import. A build tool performs module resolution: it finds the package in node_modules, locates the correct file, and rewrites or bundles the import so the browser can fetch it.
Many npm packages are also published in older module formats such as CommonJS (require). Browsers do not support those. The build tool converts them to ES modules.
Problem 3: too many requests and too much code
In production you want to ship as little as possible. A build tool helps in several ways:
| Task | What it does | Why it matters |
|---|---|---|
| Bundling | Combines many modules into a few files | Fewer network requests and a predictable load order |
| Minification | Removes whitespace and shortens variable names | Smaller files download faster |
| Tree shaking | Drops exports that nothing imports | Unused library code does not reach users |
| Code splitting | Splits output into chunks loaded on demand | Users download only the code a page needs |
| Hashed file names | Adds a content hash, such as `index-4f3a9c.js` | Browsers can cache files for a long time and still pick up new versions |
| Asset handling | Processes CSS and images, and inlines small files | Fewer requests and consistent URLs |
Problem 4: a good development experience
During development you want to save a file and see the change at once. That requires more than compiling. The tool must watch files, recompile only what changed, and push the update to the open page. When it can, it should update one component without losing the state of the rest of the app. This is called hot module replacement (HMR), and a later chapter covers it in detail.
What a build tool does, in order
The following diagram shows the path from your source files to a running page.
Do you really need one?
You can run React without a build tool. You load React from a CDN with a <script type="module"> tag and write calls such as React.createElement by hand, or use a runtime JSX compiler in the browser. This works for demos and experiments.
It breaks down for real projects:
- Compiling JSX in the browser on every page load is slow.
- You lose npm packages unless you set up import maps and host each dependency yourself.
- You get no minification, tree shaking or code splitting.
- You must wire up a dev server and live reload yourself.
For anything you plan to maintain or deploy, a build tool is the practical choice.
Common tools
Several tools do this job. They differ in speed, configuration style and how much they decide for you.
| Tool | Notes |
|---|---|
| Vite | Fast dev server that serves source modules directly. Small configuration. The subject of the rest of this tutorial. |
| webpack | Mature and highly configurable. Bundles everything before serving, and configuration can grow large. |
| Parcel | Aims for zero configuration. |
| Framework tooling | Frameworks such as Next.js ship their own build pipeline, so you rarely configure one yourself. |
The next chapter explains what Vite is and how it approaches the problem differently.
Common mistakes
- Opening
index.htmlby double-clicking it. The page loads from afile://URL, where module imports and many browser features fail. Always use the dev server, which serves overhttp://. - Putting JSX in a
.jsfile. Vite's React template expects JSX in.jsxfiles. Rename the file or the build reports a syntax error. - Treating the dev server output as the production app. The development build is unminified and includes extra warnings. Test with a production build before you deploy.
- Assuming the build tool checks types or style. It transpiles code. Linting and type checking are separate tools.
Check your understanding
Exercise
A teammate opens a React project's index.html directly from the file system, and the console shows an error about the import 'react-dom/client'. Name two reasons a browser cannot run this project as written, and say which build tool task fixes each one.
Show answer
First, the import 'react-dom/client' is a bare specifier, and the browser cannot find packages in node_modules. Module resolution in the build tool fixes this. Second, the source files contain JSX, which is not valid JavaScript. The build tool's transpile step converts JSX into plain function calls. (Opening the file over file:// also blocks module loading, so a dev server over http:// is needed.)
Exercise
A production bundle is named index-4f3a9c.js. Why does the build tool put a hash in the file name?
Show answer
The hash is derived from the file's content. When the content changes, the name changes, so browsers fetch the new file. When it does not change, the name stays the same and the browser can keep a cached copy for a long time without risking stale code.