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.

Support ESM resolution

See original GitHub issue

Hiya! 👋

We’ve discussed this briefly on Slack, but I thought an issue would be easier to keep track of.

I’m not sure what it would entail, so I’ll just list out (i.e. do a brain dump of) the features I think would be needed.

  • Should file be interpreted as CJS or ESM
    • Currently, resolve will only ever return a single string, which is the absolute path to the resolved file. I think in addition to this, we’d need to know if the file should be interpreted as CJS or ESM
    • Related, should resolve taken an option what the caller would prefer? Should it be a static option in, or some callback?
  • Support exports field in package.json
  • Support promise for async resolution
    • This is admittedly not a requirement, but it matches the Node APIs making it cleaner to use resolve when implementing custom loaders or using the VM APIs.
    • #210
  • Support URLs. Mostly because import() supports it. It probably makes more sense for resolve to return URLs than an absolute path for ESM?
  • Preserve query strings
    • Makes sense, but just adding it here as a point

There is also a flagged API in Node for this, import.meta.resolve: https://github.com/nodejs/node/pull/31032. Not sure if we should care too much about it, though?

I think that covers it, but you know way more about this subject than I do, so feel free to either close this to open up your own, or edit this OP as you see fit 👍


For background, Jest uses resolve as the default implementation in jest-resolve, although the user can swap it out. I’m currently working on support for ESM natively in Jest, and while we have a version today that sorta works, it’s not a compliant implementation. Most of the (known) issues are due to resolution logic. I’d be happy to help implement support here in resolve.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:45
  • Comments:25 (12 by maintainers)

github_iconTop GitHub Comments

7reactions
ljharbcommented, Dec 13, 2022

@IanVS yes, it’s something i very much want to complete. I’ll try to prioritize it this month.

4reactions
overlookmotelcommented, Jan 9, 2021

That said… it seems like a pretty niche case. So if it were practical to provide a sync version (with an explicit warning that it does not support async resolution like https import), it could still be useful in the majority of cases.

I am using enhanced-resolve (Webpack’s implementation) for now, which does provide sync resolution. But my understanding is that everyone will switch to resolve library once ESM support is added, and other implementations will likely fall into disrepair. So just wanted to flag sync resolution is useful for at least some users.

I’ll stop banging on about this now!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ECMAScript modules | Node.js v19.3.0 Documentation
FileURL-based resolution as is used by ES modules; Support for builtin module loading; Relative and absolute URL resolution; No default extensions; No folder ......
Read more >
What does it take to support Node.js ESM? – The Guild
To add support ESM for Node.js, you have two alternatives: ... the cache and file resolution is not the same as while using...
Read more >
Getting Started with (and Surviving) Node.js ESM
The Node.js ecosystem is shifting towards ESM. We examine what lies in store for application and library authors, learn some of challenges ...
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
For the last few years, Node.js has been working to support running ECMAScript modules (ESM). This has been a very difficult feature to...
Read more >
ECMAScript Modules - webpack
Webpack supports processing ECMAScript Modules to optimize them. Exporting. The export keyword allows to expose things from an ESM to other modules: export ......
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