You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.ts files run as ES modules under tsconfig module: node16/node18/nodenext, ignoring package.json "type" #907
Expected:ok function. Under node16, node18 and nodenext, tsc emits each .ts file in the format its package.json"type" gives it, and a package without "type" is CommonJS. So tsc compiles this file to CommonJS. Node's own type stripping (node ./entry.ts) prints the same.
Actual:ReferenceError: require is not defined in ES module scope, you can use import instead
TypeScript's own CommonJS syntax fails earlier, when the file is transformed:
Error: Failed to transform …/entry.ts:
! TS(1202): Import assignment cannot be used when targeting ECMAScript modules.
tsc compiles this to const os = require("node:os");.
The same happens to a CommonJS .ts file imported from an ES module, and to test files run with --test. module: node20 is not affected, because there the package type already decides.
Cause
default_module_from_tsconfig (src/lib.rs) groups node16, node18 and nodenext with the ES module settings (es6, es2015 … esnext). That makes the resolve hook report every .ts/.tsx file as an ES module under those settings.
Activity
test(core): pin node20 and `import =` CommonJS under node* modules
fix(core): let package.json "type" decide .ts format under node* modu…