Skip to content

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

Closed as not planned

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

Activity

  1. jakebailey commented on Feb 3, 2024

    @jakebailey
    Member

    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.

    The next version of TypeScript includes #56785, which will be the new default in VS Code and probably fix your issue. Though, it doesn't seem like the change has been made to VS Code yet.

    I wanted to suggest a temporary workaround of manually setting the option, but it looks like there's no such toggle in VS Code (only "module" alone https://github.com/microsoft/vscode/blob/7cc042767b50448d36367e6061f43eea2049c685/extensions/typescript-language-features/src/tsconfig.ts#L59).

  2. added
    Needs Investigation
    on Feb 5, 2024
  3. andrewbranch commented on Feb 5, 2024

    @andrewbranch
    Member

    Yeah, what Jake Bailey (@jakebailey) said—the defaults for VS Code are changing soon, it’s expected behavior for the defaults today, and you can change them by making a jsconfig.json if you need to.

  4. mmkal commented on Feb 6, 2024

    @mmkal
    Author

    Great- thanks!

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

Metadata

Metadata

Labels

Needs Investigation

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions