Repository navigation
Replies: 1 comment
|
A You can keep your entry point CommonJS and load the ESM dependency through a native dynamic import. Here is a TypeScript-compiler recipe that does not use SWC: {
"compilerOptions": {
"target": "ES2021",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true
},
"ts-node": {
"swc": false
}
}Save this as async function main() {
const { format } = await import('d3-format');
console.log(format(',.2f')(1234.5));
}
void main();Run I tested ts-node 10.9.2, TypeScript 5.9.3 and d3-format 3.1.0 on Node 24.19.0: it printed |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I'm trying to get a project working with a mix of CommonJS (that I can't migrate to ESM) and ESM dependencies, in this case
d3-format.I've gotten the require to point to the
distfolder usingI do have
"require": ["tsconfig-paths/register"],But this gives me
d3-format.min.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.I've tried a few different "moduleTypes" but none of them seem to apply to the
require()Any help would be appreciated. We're also using
"swc": trueif that matters.Related: #2077 (comment)
All reactions