-
Notifications
You must be signed in to change notification settings - Fork 12.8k
TS Build api failed when using node ESM module (mjs) #56366
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
The The actual problem here is the use of a native namespace import to import TypeScript (without the ES module interop helpers, as you have written I'm not sure why |
I'm not sure to understand the difference between I confirm it work using this other syntax, so is in my cli tool. I close the issue, as it's probably not a bug. Many thanks for highlighting the wrong import. |
This comment was marked as outdated.
This comment was marked as outdated.
A small example: // mod.cjs
const exported = { value: 1234 }
console.log("hello from mod.cjs! exported =", exported);
module.exports = exported;
// index.mjs
import * as asNamespace from "./mod.cjs";
import asDefault from "./mod.cjs";
console.log({ asNamespace, asDefault }); Prints:
So, what I thought was weird was in fact not (and I should have logged the whole object and not ust one prop). When you do the namespace import, you get an object with only But, I can't actually seem to get TS to emit some sort of static error to complain about this: Nor can I figure out how to get Node to emit some error here. |
Going to reopen this just because I want to still investigate some way we can help people avoid this footgun. |
Actually, the mjs and cjs files are not manually written in my case. This results from compiling a ts file (src/index.tx in my repro repo). Maybe typescript itself should detect the case ? Thanks for your detailed explanations anyways. |
Do you have |
Yes I do. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
I looked at this harder and realized that the error I previously saw was when you try and do something like: import { isSourceFile } from "typescript"; This will give an error like:
But,
So it's "correctly" detecting that we don't have any static exports, but them being empty doesn't mean "no namespace imports allowed". Downloading TypeScript 4.9 and running |
I realized that we could use the same trick esbuild does and sent #57133, which fixes this issue, allowing named imports and making the namespace import actually work. |
π Search Terms
ESM ts.sys undefined
π Version & Regression Information
β― Playground Link
https://github.com/stevebeauge/tsc-api-esm
π» Code
π Actual behavior
If I transpile this code into CJS and run
node index.cjs
works as expected (tsc.sys is defined).If I transpile this code into ESM and run
node index.mjs
the code does not works as expected (tsc.sys is undefined).π Expected behavior
tsc.sys (
System
) should be available in CLI tools, whether the output is ESM or CJS.Additional information about the issue
The issue occurs when trying to create a CLI tool that require parsing tsconfig files, including potential "extends". Typescript provides an API for that.
However, my cli tool requires to be ESM because of some imports not available as CJS.
Repro steps (using pnpm but it does not matter):
When using cjs : OK
When using ESM : KO
My investigations leds me to :
TypeScript/src/compiler/core.ts
Line 2748 in 80ab111
It populates the tsc.sys when the function "isNodeLikeSystem" returns true. However, the test expect to have
module
be defined (which is true in CJS, not in ESM).The text was updated successfully, but these errors were encountered: