Skip to content

TS Build api failed when using node ESM module (mjs)Β #56366

@stevebeauge

Description

@stevebeauge

πŸ”Ž Search Terms

ESM ts.sys undefined

πŸ•— Version & Regression Information

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

⏯ Playground Link

https://github.com/stevebeauge/tsc-api-esm

πŸ’» Code

import * as tsc from "typescript";

const tsSystem = tsc.sys;

console.log({ tsSystem });

πŸ™ 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):

git clone https://github.com/stevebeauge/tsc-api-esm
cd tsc-api-esm
pnpm i 
node index.cjs
node index.mjs

When using cjs : OK
When using ESM : KO

My investigations leds me to :

&& typeof module === "object";

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).

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions