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

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

Closed
mmkal opened this issue Feb 3, 2024 · 3 comments
Closed

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

mmkal opened this issue Feb 3, 2024 · 3 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@mmkal
Copy link

mmkal commented Feb 3, 2024

🔎 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

@jakebailey
Copy link
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).

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 5, 2024
@andrewbranch
Copy link
Member

Yeah, what @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.

@andrewbranch andrewbranch closed this as not planned Won't fix, can't repro, duplicate, stale Feb 5, 2024
@mmkal
Copy link
Author

mmkal commented Feb 6, 2024

Great- thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

4 participants