Registry / type-stubs / fix-dts-default-cjs-exports

fix-dts-default-cjs-exports

JSON →
library1.0.1jsnpmunverified

This utility addresses a common issue in TypeScript declaration generation, specifically when dealing with default exports within CommonJS modules. It provides a programmatic API and a Rollup plugin to correctly transform `d.mts` files into CommonJS-compatible `d.ts` and `d.cts` files, ensuring interoperability. The current stable version is 1.0.1, with recent releases indicating an active maintenance cadence primarily focused on bug fixes and compatibility. Key differentiators include its direct integration via a Rollup plugin, making it suitable for build tools like `unbuild` (which leverages it internally since v3.5.0), and its specific focus on the nuanced problem of default exports in CJS TypeScript declarations where other bundlers or compilers might produce incorrect outputs. It provides a targeted solution for a specific TypeScript build problem, rather than being a general-purpose bundler.

npm install fix-dts-default-cjs-exports
INSTALL
IMPORT
SIG · FIX-DTS-DEFAULT-CJ
F
fix-dts-default-cjs-exports
type-stubsjavascriptv1.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fixDtsDefaultCjsExports
import { fixDtsDefaultCjsExports } from 'fix-dts-default-cjs-exports'
const { fixDtsDefaultCjsExports } = require('fix-dts-default-cjs-exports')
This is the programmatic API function for direct usage.
FixDtsDefaultCjsExportsPlugin
import { FixDtsDefaultCjsExportsPlugin } from 'fix-dts-default-cjs-exports/rollup'
import { FixDtsDefaultCjsExportsPlugin } from 'fix-dts-default-cjs-exports'
The Rollup plugin is exposed via a subpath import to avoid polluting the main entry point. Ensure you import from `/rollup`.
PluginOptions
import type { PluginOptions } from 'fix-dts-default-cjs-exports/rollup'
Type imports should also reference the `/rollup` subpath for the plugin-specific options.

This quickstart demonstrates how to integrate the Rollup plugin with `unbuild` to ensure correct TypeScript declaration output for default CommonJS exports. This configuration is for `unbuild` versions prior to v3.5.0 or when needing custom plugin options.

import { FixDtsDefaultCjsExportsPlugin } from 'fix-dts-default-cjs-exports/rollup'; import { defineBuildConfig } from 'unbuild'; export default defineBuildConfig({ entries: ['./src/index'], declaration: true, clean: true, rollup: { emitCJS: true }, hooks: { 'rollup:dts:options': (ctx, options) => { // Optional: Uncomment to remove unbuild's internal plugin if it conflicts or you need full control // options.plugins = options.plugins.filter((p) => { // if (!p || typeof p === 'string' || Array.isArray(p) || !('name' in p)) // return true; // return p.name !== 'unbuild-fix-cjs-export-type'; // }); options.plugins.push(FixDtsDefaultCjsExportsPlugin({ warn: message => ctx.warnings.add(message) })); } } });
Debug
Known issues
breakingVersion 1.0.0 introduced breaking changes related to the inclusion of Node.js 10 types. Users upgrading from pre-1.0.0 versions should verify their build environment's Node.js compatibility.
fix
Ensure your project's `tsconfig.json` and build environment are compatible with the Node.js 10 type inclusions, or adjust `target` and `lib` settings as necessary.
affects: >=1.0.0
gotchaWhen using the Rollup plugin with `unbuild`, you *must* register the plugin directly when `rollup.emitCJS = true` is enabled. Failing to do so can lead to incorrect transformations or the plugin not being applied effectively.
fix
Ensure `rollup.emitCJS: true` is set in your `unbuild` configuration and the `FixDtsDefaultCjsExportsPlugin` is explicitly added to `options.plugins` within the `rollup:dts:options` hook, as shown in the quickstart.
affects: >=0.0.1
gotchaThe package's Rollup plugin is intended to fix default CJS exports. It does not control file generation itself. Misconfiguration of your build tool's declaration options (e.g., `unbuild`'s `declaration` option) can still lead to unexpected output files.
fix
Consult the `declaration` option in your build tool's documentation (e.g., `unbuild`'s configuration) to ensure correct file output paths and types (`d.ts`, `d.cts`, `d.mts`).
affects: >=0.0.1
gotchaThis package is not directly compatible with build tools like `tsup` or `pkgroll` as they currently do not expose hooks to replace or augment their internal Rollup configurations. Direct integration is limited to tools that allow Rollup plugin injection.
fix
For `tsup` or `pkgroll`, you may need to use a post-build script to apply the `fixDtsDefaultCjsExports` API programmatically, or wait for these tools to introduce plugin hooks.
affects: >=0.0.1
gotchaVersion 1.0.1 added a `// @ts-ignore` annotation to default exports when mixed exports are present. While this fixes a specific bug, developers should be aware that `ts-ignore` masks potential TypeScript issues and should be understood in the context of the CJS/ESM interop problem this library solves.
fix
No direct fix needed, but be aware of the annotation. If encountering unexpected TypeScript errors after upgrading, review how mixed exports are handled in your declaration files.
affects: >=1.0.1
Errors
Common errors & fixes
Cannot find name 'FixDtsDefaultCjsExportsPlugin'.
Incorrect import path for the Rollup plugin.
fix
Ensure you are importing the plugin from the correct subpath: `import { FixDtsDefaultCjsExportsPlugin } from 'fix-dts-default-cjs-exports/rollup'`.
Error: The plugin 'unbuild-fix-cjs-export-type' was already used.
You might be trying to add `fix-dts-default-cjs-exports` plugin to `unbuild` which already has an internal, potentially conflicting, plugin for the same purpose (especially in older `unbuild` versions).
fix
If using an `unbuild` version older than 3.5.0, you may need to filter out `unbuild`'s internal plugin within the `rollup:dts:options` hook before pushing the `fix-dts-default-cjs-exports` plugin, as commented in the quickstart example.
TypeScript declarations are incorrect for default exports in CommonJS output.
The `fix-dts-default-cjs-exports` plugin is not correctly configured or not being applied in your build process, or your build tool is not supported.
fix
Verify that the plugin is correctly integrated as a Rollup plugin, especially within `unbuild`'s `rollup:dts:options` hook with `rollup.emitCJS` enabled. If using `tsup` or `pkgroll`, consider a post-build programmatic fix.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
rollupoptionalThe package provides a Rollup plugin for integration into build processes.
unbuildoptionalThe package is designed to integrate with unbuild, providing a Rollup plugin that unbuild v3.5.0+ uses internally, or can be manually configured for older versions.
Agent activity
24 hits · last 30 days
node
20
OpenAI (training)
1
Resources
fix-dts-default-cjs-exports — npm install fix-dts-default-cjs-exports · libregistry