Skip to content

Compiler API - ts.ModuleKind and ts.ScriptTarget is undefined #56903

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

Closed
yxshv opened this issue Dec 30, 2023 · 5 comments
Closed

Compiler API - ts.ModuleKind and ts.ScriptTarget is undefined #56903

yxshv opened this issue Dec 30, 2023 · 5 comments

Comments

@yxshv
Copy link

yxshv commented Dec 30, 2023

🔎 Search Terms

I tried search searching on the internet for

  • "ts.Modulekind undefined"
  • "ts.ScriptTarget undefined"

but found nothing that could help me :(
but i found this issue - #26535

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about

⏯ Playground Link

No response

💻 Code

This is the same code as given in the example of Typescript compiler api

import * as ts from "typescript";

const source = "let x: string  = 'string'";

let result = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS }});

console.log(JSON.stringify(result));

🙁 Actual behavior

An error occurred, saying that ts.Modulekind is undefined

let result = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS }});
                                                                                   ^

TypeError: Cannot read property 'CommonJS' of undefined
    at file:///Users/balkrishnaverma/Documents/yash/testum/src/index.mjs:5:84
    at ModuleJob.run (internal/modules/esm/module_job.js:183:25)
    at async Loader.import (internal/modules/esm/loader.js:178:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

🙂 Expected behavior

The result should've been printed

Additional information about the issue

No response

@yxshv yxshv changed the title Compiler API - ts.ModuleKind is undefined Compiler API - ts.ModuleKind and ts.ScriptTarget is undefined Dec 30, 2023
@yxshv
Copy link
Author

yxshv commented Dec 30, 2023

Ok, I replaced ts.Modulekind.CommonJS to 1, then i received a new error

let result = ts.transpileModule(source, { compilerOptions: { module: 1 }});
                ^

TypeError: ts.transpileModule is not a function

so I thought there is something wrong with my import statemenet
so i changed it to

import ts from "typescript"
// earlier was: import * as ts ...

and it worked, but still in Compiler API Wiki example it used import * as ts ... maybe its outdated?

@jakebailey
Copy link
Member

The is a duplicate of #56366.

That code only works when in a TS file such that import helpers are in use. In native ESM in Node, you must write:

import ts from "typescript";

At some point, a namespace import used to cause Node to throw, but that is no longer true and I'm not sure why. Potentially we could update the wiki to use the default import for safety as that should work elsewhere.

@yxshv
Copy link
Author

yxshv commented Dec 30, 2023

it didn't work in the typescript file either

@jakebailey
Copy link
Member

Do you have esModuleInterop enabled?

@yxshv
Copy link
Author

yxshv commented Dec 30, 2023

Do you have esModuleInterop enabled?

Oh yeah I do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants