Skip to content

The @nx/esbuild plugin provides various executors to help you create and configure esbuild projects within your Nx workspace. Below is a complete reference for all available executors and their options.

esbuild

Bundle a package for different platforms.

<app-root>/project.json:

{
//...
"targets": {
//...
"build": {
"executor": "@nx/esbuild:esbuild",
"options": {
"main": "<app-root>",
"tsConfig": "<app-root>/tsconfig.app.json",
"outputPath": "dist/<app-root>"
}
}
}
}
Terminal window
nx build <app-name>

Examples

{% tabs %} {% tab label=“CommonJS output” %}

The CommonJS format is required in some environments, such as Electron applications. By default, esbuild will use the ESM format, which is recommended for Web and Node applications. You may also output to multiple formats.

Terminal window
nx build <app-name> --format=cjs
nx build <app-name> --format=esm,cjs
nx build <app-name> # defaults to es# defaults to esm
"build": {
"executor": "@nx/esbuild:esbuild",
"options": {
"main": "<app-root>",
"tsConfig": "<app-root>/tsconfig.app.json",
"outputPath": "dist/<app-root>",
"format": ["esm", "cjs"]
}
}

{% /tab %} {% tab label=“External packages” %}

You can avoid packages from being bundled by providing the external option with a list of packages to skip.

You can also use * wildcard to match assets.

"build": {
"executor": "@nx/esbuild:esbuild",
"options": {
"main": "<app-root>",
"tsConfig": "<app-root>/tsconfig.app.json",
"outputPath": "dist/<app-root>",
"external": ["lodash", "*.png"]
}
}

{% /tab %} {% tab label=“Skip type checking” %}

Type checking is the slowest part of the build. You may want to skip type checking during build and run it as another job in CI.

"build": {
"executor": "@nx/esbuild:esbuild",
"options": {
"main": "<app-root>",
"tsConfig": "<app-root>/tsconfig.app.json",
"outputPath": "dist/<app-root>",
"skipTypeCheck": true
}
}

{% /tab %} {% tab label=“Additional esbuild options” %}

Additional esbuild options can be passed using esbuildOptions in your project configuration.

"build": {
"executor": "@nx/esbuild:esbuild",
"options": {
"main": "<app-root>",
"tsConfig": "<app-root>/tsconfig.app.json",
"outputPath": "dist/<app-root>",
"esbuildOptions": {
"legalComments": "inline"
"banner": {
".js": "// banner"
},
"footer": {
".js": "// footer"
}
}
}
}

{% /tab %} {% tabs %}

Options

OptionTypeDescriptionDefault
mainstring [required]The path to the entry file, relative to project.
outputPathstring [required]The output path of the generated files.
tsConfigstring [required]The path to tsconfig file.
additionalEntryPointsarrayList of additional entry points.[]
assetsarrayList of static assets.[]
bundlebooleanWhether to bundle the main entry point and additional entry points. Set to false to keep individual output files.true
declarationbooleanGenerate declaration (*.d.ts) files for every TypeScript or JavaScript file inside your project. Should be used for libraries that are published to an npm repository.
declarationRootDirstringSets the rootDir for the declaration (*.d.ts) files.
deleteOutputPathbooleanRemove previous output before build.true
esbuildConfigstringPath to a esbuild configuration file. See https://esbuild.github.io/api/. Cannot be used with ‘esbuildOptions’ option.
esbuildOptionsobjectAdditional options to pass to esbuild. See https://esbuild.github.io/api/. Cannot be used with ‘esbuildConfig’ option.
externalarrayMark one or more module as external. Can use * wildcards, such as ‘*.png’.
formatarrayList of module formats to output. Defaults to matching format from tsconfig (e.g. CJS for CommonJS, and ESM otherwise).["esm"]
generatePackageJsonbooleanGenerates a package.json and pruned lock file with the project’s node_module dependencies populated for installing in a container. If a package.json exists in the project’s directory, it will be reused with dependencies populated.false
metafilebooleanGenerate a meta.json file in the output folder that includes metadata about the build. This file can be analyzed by other tools.false
minifybooleanMinifies outputs.false
outputFileNamestringName of the main output file. Defaults same basename as ‘main’ file.
outputHashingstringDefine the output filename cache-busting hashing mode."none"
platformstringPlatform target for outputs."node"
skipTypeCheckbooleanSkip type-checking via TypeScript. Skipping type-checking speeds up the build but type errors are not caught.false
sourcemapstringGenerate sourcemap.
targetstringThe environment target for outputs."esnext"
thirdPartybooleanIncludes third-party packages in the bundle (i.e. npm packages).
watchbooleanEnable re-building when files change.false