Skip to content

Cannot import some packages when tsconfig.json specifies "module": "nodenext" #46770

Closed

Description

Bug Report

When a TypeScript project specifies "module": "nodenext" (or "node12") in tsconfig.json, tsc emits errors saying it can't find imported modules or that the exports aren't the right types.

I've put together a demo that shows this with the "ip" and "nullthrows" packages. "ip" doesn't ship with TypeScript declarations and needs "@types/ip", while "nullthrows" comes with declarations. The precise errors are included below but in summary, tsc can't find "ip" and it thinks "nullthrows" is not callable despite the default export being a function.

Playing around, when I add "type": "module" to nullthrows's package.json, its error goes away. When I change ip's package.json from "main": "lib/ip" to "main": "lib/ip.js", its issue goes away. Writing this out, these are likely two different issues with the shared symptom of an ESM package not being able to import either of them.

🔎 Search Terms

esm module nodenext

🕗 Version & Regression Information

typescript@4.6.0-dev.20211110

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about modules. The issue is present in the 4.5.0 betas as well, though I understand that ESM support via nodenext/node12 is being deferred.

⏯ Playground Link

Minimal repo: https://github.com/ide/typescript-esm-demo. Dependencies are needed to show the effect.

💻 Code

import * as ip from 'ip';
import nullthrows from 'nullthrows';

export function getAddress(): string {
  return nullthrows(ip.address());
}

🙁 Actual behavior

tsc outputs:

index.ts:1:21 - error TS2307: Cannot find module 'ip' or its corresponding type declarations.

1 import * as ip from 'ip';
                      ~~~~

index.ts:5:10 - error TS2349: This expression is not callable.
  Type 'typeof import("/private/tmp/typescript-esm-demo/node_modules/nullthrows/nullthrows")' has no call signatures.

5   return nullthrows(ip.address());
           ~~~~~~~~~~


Found 2 errors.

🙂 Expected behavior

tsc should compile without errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

BugA bug in TypeScriptFix 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