Cannot compile a project as both Node ESM and Node CJS without hacky workarounds #55925
Closed
1 task done
Labels
Duplicate
An existing issue was already created
Acknowledgement
Comment
I have a project that I want to compile twice, once emitting code targeting commonjs and the second emitting code targeting Node compatible module imports.
My objective is to distribute a library where the import format can be conditionally loaded through the use of Node's conditional exports capability.
To maximise compatibility, the library would be distributed as
type: commonjs
and themain
property which points to the commonjs output.I need TypeScript to validate that file extensions are present in imports so the emitted code is compatible with Node ESModules.
To achieve the differential loading, I need to use Node's conditional loading via
exports
I have a single
src
that is compiled to the target based on the compiler configuration:and I attempt to override the relevant properties in-line on the scripts
The issue is,
module: Node16
determines the output format based on thetype
property of thepackage.json
.This means I must programmatically rewrite the
package.json['type']
field to instructtsc
to emit CJS or MJS code and cannot rely on a TypeScript configuration compilerOption.Intuitively, I would have thought:
However, in conjunction with the
package.json
type
field (which cannot be easily set programmatically), the above combinations are invalid and you can only use:The text was updated successfully, but these errors were encountered: