Skip to content

Types not found when importing one ESM package from another #57279

Closed as not planned
@mmkal

Description

@mmkal

🔎 Search Terms

"package.json exports types"

I found this, which is similar, but I don't think the same thing. #50762

🕗 Version & Regression Information

I see this on VSCode's typescript version, 5.3.2. It only really affects JavaScript, because with TypeScript you will usually have a tsconfig.json - and setting moduleResolution to node16 fixes.

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about package.json (they don't exist)

⏯ Playground Link

https://www.typescriptlang.org/play?ssl=1&ssc=1&pln=2&pc=1#code/JYWwDg9gTgLgBCAphA7nAZlCI4HImq4BQRoksCyKAkgHYyJS0CGANhljvlQPQBGAV2CsAJj2C0RiAB4A6AFYBnYkA

💻 Code

Note that the playground link starts working as expected if you set the tsconfig.json to moduleResolution=node16

import meow from 'meow'

import meowInternal from 'meow/build/index.js'

You can also see this problem by opening np in vscode:

git clone https://github.com/sindresorhus/np
cd np
npm install
code .

Then opening source/cli-implementation.js.

🙁 Actual behavior

Cannot find module 'meow' or its corresponding type declarations.(2307) showing as an intellisense error in IDEs.

🙂 Expected behavior

No intellisense error in IDEs. I'm guessing typescript comes up with a default jsconfig.json - and for codebases with a "type": "module" package.json (like np), it should assume moduleResolution=node16 or some similar value that looks for types in package.json -> exports.types, not just package.json -> types.

Additional information about the issue

If you look at meow's package.json, it is correctly configured: https://github.com/sindresorhus/meow/blob/fd0bc62ce47781e11da506b8e38e8668eb78a584/package.json

{
	"name": "meow",
	"version": "13.1.0",
	...
	"type": "module",
	"exports": {
		"types": "./build/index.d.ts",
		"default": "./build/index.js"
	},
	...
}

Adding a types field at the root level also fixes, but the maintainer didn't want to add a workaround to a typescript bug: sindresorhus/meow#253

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions