-
Notifications
You must be signed in to change notification settings - Fork 12.8k
CommonJS globals permitted for ES module builds with no compiler error. #58658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
If when targeting What is the recommended usage pattern here? I know people when writing ES modules often do something like this const __dirname = dirname(fileURLToPath(import.meta.url)) |
This is more or less expected as these are defined by |
Thanks for the feedback. I guess I'm wondering why the compiler doesn't introduce some heuristic to at least warn about using CJS globals when targeting ES module output based on the |
TS does not have a concept of "warnings", though a "suggestion" diagnostic is close. It's also non-trivial because people frequently write ESM with an assumption about transpilation (but maybe want to There are lint rules that can be strict about this, if you fit within their guidelines. |
Another interesting thing about the compiler behavior is Given: cjs.ts exports.foo = 'bar'; package.json "type": "module" file.ts import './cjs.js' With exports.foo = 'bar';
export {}; Also, there is no compiler error, only a runtime error. Is this somehow related to #56678? |
I am working on converting a Node.js TypeScript project from CJS to ESM and noticed a related issue with a dynamic Using TypeScript, I'm relying heavily on I'm expecting that This can be replicated with this tconfig.json file:
and using Node.js v20.17.0 |
This would be a very useful feature. |
π Search Terms
nodenext, module, __dirname
π Version & Regression Information
Version 5.4.5
β― Playground Link
No response
π» Code
package.json
tsconfig.json
file.ts
π Actual behavior
No compiler error, but this causes a runtime error in Node:
π Expected behavior
That the compiler issues an error similar to the inverse situation.
For example, when targeting CommonJS:
package.json
file.ts
The compiler issues the following error:
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 install
npm run esm
(note no compile error but the output causes a runtime error)npm run cjs
(note there is a compile error)The text was updated successfully, but these errors were encountered: