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.

Do not add default `node`, `browser`, `require` or `import` conditions

See original GitHub issue

I’m looking into using this module to implement support for exports in Jest.

Seems unfortunate that Node’s defaults should be forced on everybody using exports field. For a module that isn’t supported to run in Node, it should just fail to resolve or something (or falling back to default) not pick up node.

There’s already a switch in this library for browser condition so it’s not consistently enforced (at least from my understanding browser is just a thing Node acknowledges exists, nowhere in the spec does it say its presence removes the node condition) - why not just fully let the caller provide their own conditions (barring possibly default - that seems reasonable to always add)? I.e. remove the two allows.add calls (and maybe the options leaving only conditions). Or possibly add node if no conditions is passed, but otherwise add nothing.

As a datapoint, TypeScript could not have used resolve.exports to implement support for types condition with their official example: https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#packagejson-exports-imports-and-self-referencing since this module would have resolved import first.

I think resolve.exports should be implementation/host independent (maybe a resolve.exports-node or just a import { node } from 'resolve.exports' makes sense, but the base export shouldn’t put these limitations there).

Tl;dr: removing https://github.com/lukeed/resolve.exports/blob/c6814c43cb27ee6092f649be3fdf6c1fb55c51e4/src/index.js#L71-L72 would allow this module to be used by any runtime/host to implement exports support without getting Node semantics/conditions when the module might not even run in Node or a browser.


This is mostly a copy paste of https://github.com/yarnpkg/berry/pull/2431#discussion_r720221992.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:25 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
lukeedcommented, Oct 15, 2021

Released in 1.1.0~! Hopefully added enough warning labels for the general use case, but I guess time will tell 😆 Guess this means that TS could have used this now directly, although I’m sure they already have their recursive if ("types" in thing) checker in place.

1reaction
lukeedcommented, Oct 14, 2021

@SimenB I gotta run, but I put up a quick PR that should address your case/concerns. Tests attached that also encompasses your last snippet.

When unsafe is on, the only other option that has an interplay with it is conditions – so the idea is that by using unsafe you are directly passing in the list conditions you want to support. Only default is left on as a default condition – I think you agreed earlier that there was no logical reason to remove this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Node.js require vs. ES6 import/export - Stack Overflow
I meant that the import/export modules feature is not implemented in any browsers natively. A transpiler is still required. – snozza. Jul 4,...
Read more >
JavaScript Require vs. Import - Bits and Pieces
“Require” is built-in with NodeJS​​ However, if you are using require to get local modules, first you need to export them using module....
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
It can load JSON modules, but an import assertion is required. ... If a package.json file does not have a "type" field, .js...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
Named and default imports are distinct syntaxes in JavaScript modules. Default import. Default exports need to be imported with the ...
Read more >
Requiring modules in Node.js: Everything you need to know
The main object exported by the require module is a function (as used in the above example). When Node invokes that require() function...
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