microsoft / microsoft/TypeScript

Mechanism to specify input source location to find `package.json` instead of output source location when determining module format

Open
#63,251 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔎 Search Terms

nodenext, ts2351, construct

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about "A default import of a commonjs module with a default in a esm file doesn't seem to be the default export of that module when module is node16 or nodenext".

### ⏯ Playground Link

N/A

### 💻 Code

```
git clone https://github.com/monsanto/typescript-nodenext-bug
cd typescript-nodenext-bug
npm install
npm run tsc
```

### 🙁 Actual behavior

```
foo/index.ts:6:5 - error TS2351: This expression is not constructable.
Type 'typeof import("/typescript-nodenext-bug/.cache/foolib/class")' has no construct signatures.

6 new CBad();
~~~~

Found 1 error.
```

### 🙂 Expected behavior

It succeeds.

### Additional information about the issue

- This issue requires a composite project, `NodeNext`, a package boundary, a default export, and a parent directory with a file package.{json, json5, yaml} without `type`: `module`.
- Please note that import default inside `foo`'s package boundary works.
- One can delete the provided package.json after installing and run `node_modules/typescript/bin/tsc --build tsconfig-base.json` to test without `package.json` present.
- One can see other package extensions trigger the bug by `mv package.json package.yaml` and `pnpm run tsc`.
- This is not a CommonJS project. It doesn't matter that there is a parent `package.json` without it. Node doesn't even read `package.yaml`.
- This is from a real-world monorepo where there is a root pnpm `package.json5` for cross-package scripts.

Cross-ref for #62200

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked typescript-nodenext-bug reproduction: run npm install and npm run tsc, then inspect tsconfig-base.json and the package.json, package.json5, and package.yaml variants described in the issue. Compare the NodeNext module-format result when the parent package metadata is present or absent; done means the reproduction succeeds without TS2351.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
build-system, compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.