Node.js ESM seems work differently than TypeScript.
It's resulting in programs that pass typechecking and yet crash at runtime.
package.json
{
"dependencies": {
"@types/lodash": "^4.0.0",
"lodash": "^4.0.0",
"typescript": "^4.0.0"
},
"type": "module"
}
tsconfig.json
{
"compilerOptions": {
"module": "es2015"
}
}
a.ts
import { partition } from "lodash";
partition([1, 2, 3, 4], n => n % 2);
Run
npm install
node_modules/.bin/tsc -p .
node index.js
And the program crashes:
file:///home/paul/test44/index.js:1
import { partition } from "lodash";
^^^^^^^^^
SyntaxError: Named export 'partition' not found. The requested module 'lodash' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'lodash';
const { partition } = pkg;
at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:179:5)
at async Loader.import (node:internal/modules/esm/loader:178:24)
at async Object.loadESM (node:internal/process/esm_loader:68:5)
at async handleMainPromise (node:internal/modules/run_main:63:12)
How can I bring TypeScript and Node.js ESM into alignment, so that typechecking matches runtime behavior?
EDIT:
I have tried using typescript@next with "module": "nodenext".
The same error happens.