-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
π Search Terms
nodenext, module, __dirname
π Version & Regression Information
Version 5.4.5
β― Playground Link
No response
π» Code
package.json
"type": "module"tsconfig.json
{
"compilerOptions": {
"module": "NodeNext"
},
"include": ["src"]
}file.ts
console.log(__dirname)π Actual behavior
No compiler error, but this causes a runtime error in Node:
ReferenceError: __dirname is not defined in ES module scope
π Expected behavior
That the compiler issues an error similar to the inverse situation.
For example, when targeting CommonJS:
package.json
"type": "commonjs"file.ts
console.log(import.meta.dirname)The compiler issues the following error:
error TS1470: The 'import.meta' meta-property is not allowed in files which will build into CommonJS output.
Additional information about the issue
Perhaps there is a good reason for this that I'm not understanding, but I would think tsc should issue an error or warning for any syntax that would produce a runtime error.
Here is a more complete example: https://github.com/knightedcodemonkey/tsc-module-globals
npm installnpm run esm(note no compile error but the output causes a runtime error)npm run cjs(note there is a compile error)
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript