-
Notifications
You must be signed in to change notification settings - Fork 170
Closed
Labels
Description
Right now this module doesn't support ESM. If you try to use modules, your function will not load.
Provided module can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/timmerman/Documents/trash/esmtestff/index.js
require() of ES modules is not supported.
I believe there are a few things we need to do to allow functions using modules to be supported:
Repro
Allow modules in package.json
"type": "module",
Use ESM import/export
/**
* Send "Hello, World!"
* @param req https://expressjs.com/en/api.html#req
* @param res https://expressjs.com/en/api.html#res
*/
export const helloWorld = (req, res) => {
res.send('Hello, World!');
};
Observe error:
Provided module can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: esmtestff/index.js
require() of ES modules is not supported.
Solution
Here's the rough solution I think, after prototyping a bit:
loader.js#getUserFunction
:- Await
const functionModulePath = await getFunctionModulePath(codeLocation);
loader.js#getFunctionModulePath
- We need to determine if the user code is a module or cjs.
- if module,
await import(codeLocation)
; wherecodeLocation is full file (including index.js part)
. - else,
require.resolve(codeLocation)
, normal require
- if module,
- We need to determine if the user code is a module or cjs.
- Await
index.js
: We need to async/awaitgetUserFunction
:
CC: @bcoe
Utwo, kolban-google, taeold, todorone, DibyodyutiMondal and 14 more