Skip to content

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

Description

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

Activity

  1. weswigham commented on Feb 11, 2022

    @weswigham
    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.

  2. weswigham commented on Feb 11, 2022

    @weswigham
    Member

    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.

  3. weswigham commented on Feb 11, 2022

    @weswigham
    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.

  4. added
    BugA bug in TypeScript
    Domain: ES ModulesThe issue relates to import/export style module behavior
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Feb 11, 2022
  5. wight554 commented on Apr 25, 2022

    @wight554

    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

  6. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: ES ModulesThe issue relates to import/export style module behaviorFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions