esbuild-plugin-license
raw JSON → 1.2.3 verified Fri May 01 auth: no javascript
esbuild plugin to generate license files for bundled dependencies. Current stable version is 1.2.3. It is actively maintained with regular releases. Key differentiators: generates both a banner for the bundle and a separate third-party license file, uses a template system based on EJS and offers a custom template function. Compared to alternatives like rollup-plugin-license, it is designed specifically for esbuild.
Common errors
error Error: esbuild-plugin-license: Cannot read properties of undefined (reading 'packageJson') ↓
cause Template function signature changed in v1.1.0; the first parameter is still dependencies but the entry package is now in the second parameter 'self'.
fix
Update template function to accept two parameters:
function(dependencies, self) and use self.packageJson for the entry package. error Error: The plugin 'esbuild-plugin-license' must be imported as default. ↓
cause Using named import instead of default import.
fix
Replace
import { esbuildPluginLicense } with import esbuildPluginLicense. error Error: esbuild-plugin-license: Expected a function for 'template' option, got object. ↓
cause The 'template' option receives a function returning a string, not a template string or object.
fix
Provide a function:
template(dependencies) { ... return string; }. Warnings
breaking In v1.1.0, the entry package is no longer included as a dependency in the third-party output; instead it is provided separately as 'self' in the template function. ↓
fix Update template function signature to use the second parameter 'self' for the entry package.
breaking In v1.1.0, when multiple versions of the same package exist, the highest version is now used deterministically instead of undefined behavior. ↓
fix No action needed; output may change if duplicates existed.
gotcha Default export is used; named import syntax `import { esbuildPluginLicense }` will fail. ↓
fix Use default import: `import esbuildPluginLicense from 'esbuild-plugin-license'`.
gotcha The plugin does not work with esbuild's `format: 'esm'` if you rely on `__dirname` or `require` in Node.js; bundle must be compatible. ↓
fix Set `platform: 'node'` and ensure the bundle does not use ESM-only features that break Node CJS interop.
Install
npm install esbuild-plugin-license yarn add esbuild-plugin-license pnpm add esbuild-plugin-license Imports
- esbuildPluginLicense wrong
import { esbuildPluginLicense } from 'esbuild-plugin-license'correctimport esbuildPluginLicense from 'esbuild-plugin-license' - Options wrong
import { Options } from 'esbuild-plugin-license'correctimport type { Options } from 'esbuild-plugin-license' - Dependency
import type { Dependency } from 'esbuild-plugin-license'
Quickstart
import * as esbuild from 'esbuild';
import esbuildPluginLicense from 'esbuild-plugin-license';
await esbuild.build({
entryPoints: ['src/index.ts'],
outdir: 'dist',
bundle: true,
platform: 'node',
plugins: [
esbuildPluginLicense({
banner: '/*! <%= pkg.name %> v<%= pkg.version %> */',
thirdParty: {
includePrivate: false,
output: {
file: 'dependencies.txt',
template(dependencies) {
return dependencies
.map(d => `${d.packageJson.name}:${d.packageJson.version} -- ${d.packageJson.license}`)
.join('\n');
},
},
},
}),
],
});