How to Install esbuild and Set Up Your First Build
In short Install esbuild per project as a dev dependency, with the exact version pinned: npm install --save-dev --save-exact esbuild npx esbuild --version After that, one command builds your first bundle: n
In short
Install esbuild per project as a dev dependency, with the exact version pinned:
npm install --save-dev --save-exact esbuild
npx esbuild --version
After that, one command builds your first bundle:
npx esbuild src/index.ts --bundle --outfile=dist/index.js
For watch mode, plugins and anything beyond a few flags, switch to a small build script using the JavaScript API. How that works is shown below. I have been building my own packages and tools this way for years.
Requirements
- Node.js and npm in a current LTS version
-
A project with a
package.json. If you don't have one yet,npm init -ycreates it.
esbuild itself is written in Go. During installation the npm package fetches the matching binary for your system (macOS, Linux, Windows, each on x64 or ARM). You don't need a Go compiler.
Install esbuild
npm install --save-dev --save-exact esbuild
-
--save-devadds esbuild todevDependencies. You only need it to build, not at runtime. -
--save-exactwrites the exact version without^intopackage.json. esbuild is still at version 0.x and ships larger changes in new minor versions, so the official guide pins the version exactly. Updates then happen on purpose.
Check that everything is in place:
npx esbuild --version
Tip: A global installation (
npm install -g esbuild) works too, but then every project builds with the same version. Installed locally, each project has its own, andnpm cion the build server gets exactly the same one.
Your first bundle
Say your code lives in src/index.ts:
npx esbuild src/index.ts --bundle --outfile=dist/index.js
--bundle pulls every import (including those from node_modules) into one file, --outfile sets where it is written. A few flags you will need often:
| Flag | Effect |
|---|---|
--minify |
shrink the code, for the production build |
--sourcemap |
write a source map so errors point to your source code |
--platform=node |
build for Node.js instead of the browser |
--format=esm |
output ES modules instead of an IIFE |
--outdir=dist |
a folder instead of a file, needed for several entry points |
esbuild understands TypeScript and JSX without any configuration. It only strips the types, though, and does not check them. Run tsc --noEmit as well, for example in your test script.
As an npm script
So you don't have to remember the flags, put them into package.json:
{
"scripts": {
"build": "esbuild src/index.ts --bundle --minify --sourcemap --outfile=dist/index.js",
"watch": "esbuild src/index.ts --bundle --sourcemap --outfile=dist/index.js --watch"
}
}
Then npm run build or npm run watch is all you need. npm scripts don't need npx, npm finds esbuild in node_modules/.bin on its own.
Watch mode
With --watch, esbuild rebuilds on every change and reports [watch] build finished, watching for changes.... Stop it with Ctrl+C.
A build script with the JavaScript API
As soon as you need plugins, the command line is no longer enough: plugins only work through the JavaScript API. Create a build.mjs:
import * as esbuild from 'esbuild';
await esbuild.build({
entryPoints: ['src/index.ts'],
bundle: true,
minify: true,
sourcemap: true,
outfile: 'dist/index.js',
});
And run it with node build.mjs. The .mjs extension makes Node.js understand import. If your package.json already contains "type": "module", the file may be called build.js as well.
Watch mode works through the API with a context:
const ctx = await esbuild.context({
entryPoints: ['src/index.ts'],
bundle: true,
outfile: 'dist/index.js',
});
await ctx.watch();
Adding plugins
Three things are missing from esbuild in almost every project of mine, so I wrote small plugins for them:
-
esbuild-plugin-clear empties the output folder before every build, so no old files are left behind in
dist/. -
esbuild-plugin-copy copies static files such as
index.html, images orrobots.txtinto the output folder after every build. - esbuild-plugin-time shows in the console when a build starts and how long it took, which is especially handy in watch mode.
npm install --save-dev esbuild-plugin-clear esbuild-copy-plugin esbuild-plugin-time
A build script with all three that runs in watch mode with --watch:
import * as esbuild from 'esbuild';
import clear from 'esbuild-plugin-clear';
import copy from 'esbuild-copy-plugin';
import time from 'esbuild-plugin-time';
const options = {
entryPoints: ['src/index.ts'],
bundle: true,
sourcemap: true,
outfile: 'dist/index.js',
plugins: [
clear(),
copy({from: './public'}),
time('App'),
],
};
if (process.argv.includes('--watch')) {
const ctx = await esbuild.context(options);
await ctx.watch();
}
else {
await esbuild.build(options);
}
time() comes last on purpose: after the build, esbuild calls the plugins in the order of the array, so time() includes the copying in its measurement. The plugins need Node.js 22.13 or later and esbuild 0.19 or later; the details are in each plugin's docs.
Common errors
| Message | Fix |
|---|---|
esbuild: command not found |
esbuild is only installed locally. Use npx esbuild or an npm script. |
Cannot use import statement outside a module |
The build script ends in .js without "type": "module". Rename it to build.mjs. |
You installed esbuild for another platform than the one you're currently using |
node_modules was copied between systems, e.g. from a Mac into a Docker container. Delete node_modules and reinstall there with npm ci. |
| Type errors don't show up | esbuild does not check types. Run tsc --noEmit as well. |
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.