question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

package.json `exports` resolution uses fallback conditions, unlike Node

See original GitHub issue

Bug Report

🔎 Search Terms

I’ve never been more confident, without searching, that I’m the first person to notice this

💻 Code

{
  "exports": {
    ".": {
      "import": "./dist/main.mjs",
      "types": "./dist/foo.d.ts"
    }
  }
}

🙁 Actual behavior

Importing this package from an ESM file, in --moduleResolution nodenext, searches for types at these locations:

  • ./dist/main.mts
  • ./dist/main.d.mts
  • ./dist/foo.d.ts

🙂 Expected behavior

It should not search the types condition, because import already matched, and contained a valid target ./dist/main.mjs. Resolution here should fail for consistency with Node, which would throw a resolution error if it matched a condition but then failed to find the file specified in it.

Fixing this bug may cause more harm than good. I noticed it because I claimed that a popular library would be broken under certain conditions due to misconfigured exports, but was proven wrong in reality. I figured this may be worth documenting, but likely not worth fixing. But if someone can make a good case for why it matters, I would reconsider.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
Andaristcommented, Sep 28, 2022

@hayes I was just working around this exact issue earlier today. You can check out what we have settled on at the end in this PR: https://github.com/alexreardon/tiny-invariant/pull/150/files

With the types condition being used first it’s implied that this package is treated as a CJS package (it has no package.json#type set). And importing default export from a CJS package doesn’t work in node - it returns the namespace object instead of the default export.

1reaction
andrewbranchcommented, Sep 28, 2022

That’s correct; https://github.com/hayes/ts-esm-cjs-nodenext is unrelated to this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Package exports - webpack
The exports field in the package.json of a package allows to declare which module should be used when using module requests like import...
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
The "exports" provides a modern alternative to "main" allowing multiple entry points to be defined, conditional entry resolution support between environments, ...
Read more >
Announcing TypeScript 4.7 - Microsoft Developer Blogs
package.json Exports, Imports, and Self-Referencing. Node.js supports a new field for defining entry points in package.json called ...
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
type in package.json and New Extensions · import / export statements and top-level await can be used · relative import paths need full...
Read more >
Configuring Jest
To read TypeScript configuration files Jest requires ts-node . ... be defined through the "jest" key in the package.json of your project:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found