Search code examples
javascriptnode.jstypescriptcommonjs

Import es module file from CommonJS TypeScript


I'm trying to import a package where its package.json specifies "type": "module". I saw Can Typescript import CommonJS Modules?, but its solution (--allowJs) does not work for me.

I've tried setting various values in my tsconfig.json for module, moduleResolution, allow allowJs. Both esModuleInteropt and allowSyntheticDefaultImports are set to true.

I've tried using import ... from, import(...).then(), import ... = require(...), and renaming my index.js and the package's index.js to index.cjs. They all result in the same error when I run in node:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: E:\src\...\node_modules\stringify-object\index.js
require() of ES modules is not supported.
require() of E:\src\...\node_modules\stringify-object\index.js from E:\src\...\index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename E:\src\...\node_modules\stringify-object\index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from E:\src\...\node_modules\stringify-object\package.json.

I tried removing "type": "module" from the packages's package.json, but it relies on other packages with "type": "module".

The last time I tried to figure this out, I gave up and went with a different package. This really doesn't seem like it should be hard to do.


Solution

  • I found what I'd consider a workaround. If I set "module": "ESNext" in tsconfig.json and rename the output to index.mjs, it works. It's not particularly elegant, though. There just must be a better way.

    I'd be happy to accept a different, better answer.


    As pointed out in the comment, setting "type": "module" in my project's package.json makes it so I do not need to rename index.js to index.mjs.