Skip to content

[api-extractor] ExtractorConfig fails when bundled into ESM output #5864

Description

@EgoziE

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Needs triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions