When @microsoft/api-extractor is bundled into a single-file ESM output (e.g. with esbuild, rollup, rolldown, or webpack), the following error is thrown at runtime:
Error: File does not exist: /path/to/bundle-output/schemas/api-extractor-defaults.json
Root cause
ExtractorConfig loads its default config at module initialization time using a __dirname-relative path:
ExtractorConfig._defaultConfig = JsonFile.load(path.join(__dirname, '../schemas/api-extractor-defaults.json'));
__dirname is a CJS-only global — it doesn't exist in ESM. To handle this when bundling CJS code into ESM output, bundlers inject a replacement. For example, rolldown's recommended workaround is to globally define __dirname as import.meta.dirname via transform.define. This replacement resolves to the bundle's output directory rather than the original api-extractor package directory, so the schema file is never found.
Suggested fix
Replace the filesystem read with a static JSON import, which bundlers inline at build time:
import defaultConfig from '../schemas/api-extractor-defaults.json' with { type: 'json' };
ExtractorConfig._defaultConfig = defaultConfig;
This eliminates the runtime filesystem dependency entirely and works correctly whether the package is used directly or bundled.
When
@microsoft/api-extractoris bundled into a single-file ESM output (e.g. with esbuild, rollup, rolldown, or webpack), the following error is thrown at runtime:Root cause
ExtractorConfigloads its default config at module initialization time using a__dirname-relative path:__dirnameis a CJS-only global — it doesn't exist in ESM. To handle this when bundling CJS code into ESM output, bundlers inject a replacement. For example, rolldown's recommended workaround is to globally define__dirnameasimport.meta.dirnameviatransform.define. This replacement resolves to the bundle's output directory rather than the originalapi-extractorpackage directory, so the schema file is never found.Suggested fix
Replace the filesystem read with a static JSON import, which bundlers inline at build time:
This eliminates the runtime filesystem dependency entirely and works correctly whether the package is used directly or bundled.