Skip to content
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

TypeScript module "NodeNext" does not resolve index.d.ts from CJS modules #47848

Closed
AndrewLeedham opened this issue Feb 11, 2022 · 4 comments Β· Fixed by #47854
Closed

TypeScript module "NodeNext" does not resolve index.d.ts from CJS modules #47848

AndrewLeedham opened this issue Feb 11, 2022 · 4 comments Β· Fixed by #47854
Assignees
Labels
Bug A bug in TypeScript Domain: ES Modules The issue relates to import/export style module behavior Fix Available A PR has been opened for this issue

Comments

@AndrewLeedham
Copy link

Bug Report

πŸ”Ž Search Terms

NodeNext, esm, CJS, cli-spinners

πŸ•— Version & Regression Information

  • This only occurs in nightly builds.
  • I was unable to test this on prior versions because nodenext is only supported in the nightly versions.

⏯ Playground Link

From what I can tell you can't install npm modules in the playground, so I made a repro on StackBlitz: https://stackblitz.com/edit/node-opvt9e?file=index.ts run npm run typecheck in the console to see the issue.

πŸ’» Code

import spinners from 'cli-spinners';

console.log(spinners);

πŸ™ Actual behavior

TypeScript cannot find the typings for cli-spinners which is a CJS module. It does not use a main or exports or types field in the package.json, it just includes a index.js and index.d.ts file.

index.ts:1:22 - error TS2307: Cannot find module 'cli-spinners' or its corresponding type declarations.

1 import spinners from 'cli-spinners';
                       ~~~~~~~~~~~~~~


Found 1 error in index.ts:1

πŸ™‚ Expected behavior

New NodeNext module behaviour to import CJS as it used to. E.g looking at index.d.ts if there is no main field. https://www.typescriptlang.org/docs/handbook/module-resolution.html#how-typescript-resolves-modules

πŸ‘ͺ Related

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 11, 2022
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.7.0 milestone Feb 11, 2022
@weswigham
Copy link
Member

Huh, I didn't realize we were relying on the directory lookup behavior to fallback to find the main index.js if it wasn't explicitly listed. Shouldn't be too hard to fix.

@weswigham
Copy link
Member

weswigham commented Feb 11, 2022

Hm, it's a little bit more complicated than that - looks like node doesn't assume a index.js main for packages found via relative imports, but does for those found via nonrelative imports. (Even if both would conceptually look at the same package file under normal cjs rules!) That's... a headache.

@weswigham
Copy link
Member

I've opened both #47854 with a fix for this to match node's current behavior, and nodejs/node#41940 to get some kind of discussion about node surprising esm resolver behavior when importing packages via relative paths.

@weswigham weswigham added Bug A bug in TypeScript Domain: ES Modules The issue relates to import/export style module behavior and removed Needs Investigation This issue needs a team member to investigate its status. labels Feb 11, 2022
@wight554
Copy link

wight554 commented Apr 25, 2022

Hi, don't know if I should open new issue, so decided to report here
I've tried to switch my project to TS 4.7 with new module: nodenext and some libraries I use fail to find type declarations, here's the list:
https://github.com/facebookexperimental/Recoil
https://github.com/preactjs/preact (+ preact/hooks)
https://github.com/developit/htm
Currently I was able to shim those by declaring local module and importing type declarations from node_modules, like this

declare module "preact" {
  export * from 'node_modules/preact/src/index.js'
}

I'd like to open issues in corresponding libraries, to fix it properly
Is there any doc/issue I can use for reference, so It'd be easier to fix those by library maintainers?

Upd. fixed in preact master and it's now clear for me what to do
preactjs/preact#3513

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: ES Modules The issue relates to import/export style module behavior Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants