microsoft / microsoft/TypeScript
CommonJS globals permitted for ES module builds with no compiler error.
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
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)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with package.json, tsconfig.json, and file.ts using the referenced npm run esm and npm run cjs commands. Compare the existing CommonJS import.meta diagnostic with the ES module __dirname case; done means the NodeNext ES module build reports the invalid CommonJS global before runtime.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100