-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Cannot use mjs file synchronously in commonJS without await / .then() #33605
Comments
I see from a related PR that a function If I can just call We don't have to do anything "special" with |
Relevant discussion here nodejs/modules#454 |
And there's a PR at #30891, but it has so many issues that that's why nodejs/modules#454 was opened. It's not clear that the issues raised will be addressable; or in plainer terms, ESM code can be loaded into a CommonJS module via |
Ok, I would like to |
I've also found a userland solution const syncify = require('@snek/syncify')
const isPromise = syncify(import('./third-party-is-promise.mjs')) There are native modules on npm that implement |
Those who use |
More realistically, if you're looking for a way to "bottle up" promises and resolve them synchronously, you should look into |
The ECMAScript spec does actually provide data on if top level await is used: https://tc39.es/proposal-top-level-await/#sec-cyclic-module-records in a new internal |
Could you explain more about the limitations of loading a WASM module synchronously ? I think I would be fine with a The fast majority of the uses cases for ESM is trivial code that doesn't do anything special with top level await or WASM. |
@Raynos I am not as clear on the reasoning, but they always initialize async in module form. Someone more vested in their design/implementation would be more knowledgable. |
wasm esm modules take advantage of off-thread compilation and things of that nature, using wasm's async apis. |
I was looking at using ESM again and sharing some ideas on twitter ( https://twitter.com/Raynos/status/1285109697662640130 ). Having importSync() available would help migrate to ESM without breaking all my commonJS users. |
so, we're not ever going to have importSync. cjs to esm interop is a problem we'd like to solve but it wouldn't be in this manner, and I think we already have other issues tracking this more generally, so I'm going to close this. |
Could you link to the more general issue for using esm from cjs ? I just did a search and could not find anything. |
Oh it's not in this repository. And there's this PR which was already referenced ( #30891 ) |
What steps will reproduce the bug?
Require an mjs file.
Example https://gist.github.com/Raynos/2c6bb0a8537dbf7d464a9b85813cd3b1
How often does it reproduce? Is there a required condition?
Always
What is the expected behavior?
I can load an ESM module from a commonJS file.
What do you see instead?
Additional information
I would like to import an ESM synchronously from a commonJS file.
Node added
child_process.execSync()
a while ago but that used to not be possible.It would be really nice if node had some kind of
importSync()
feature.I also tried
This returns a promise instead of the code which is not what I want
The text was updated successfully, but these errors were encountered: