winglang/wing

Cannot use `await import()` to import ESM-only modules in inflight

eladb opened this issue · 4 comments

eladb commented

I tried this

Install nanoid and write an extern that uses it:

const { nanoid } = require('nanoid');

exports.make_id = async function() {
  return nanoid();
};

I expected this:

To work

Instead, this happened

require() of ES Module /Users/eladb/playground/nano-id/node_modules/nanoid/index.js from /Users/eladb/playground/nano-id/test.js not supported.
Instead change the require of index.js in /Users/eladb/playground/nano-id/test.js to a dynamic import() which is available in all CommonJS modules.

Is there a workaround?

Yes, you can use async import() inside your extern function to import ESM modules:

Not working!

exports.foo = async function() {
  let n = await import("nanoid");
  return n.nanoid();
};

Component

SDK

Wing Version

No response

Wing Console Version

No response

Node.js Version

No response

Platform(s)

No response

Anything else?

No response

Community Notes

  • Please vote by adding a 👍 reaction to the issue to help us prioritize.
  • If you are interested to work on this issue, please leave a comment.

Inflight bundling solves this and would allow us to mostly ignore the ESM/CJS kerfuffle

eladb commented

The workaround is not working here!

Reverted by #2632

Congrats! 🚀 This was released in Wing 0.17.26.