Support ESM
See original GitHub issueRight 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
Issue Analytics
- State:
- Created 3 years ago
- Reactions:18
- Comments:10 (6 by maintainers)
Top Results From Across the Web
What does it take to support Node.js ESM? – The Guild
I have worked on all The Guild's libraries and graphql-js to support ESM. Here is how you can do it too.
Read more >ECMAScript modules | Node.js v19.3.0 Documentation
Node.js fully supports ECMAScript modules as they are currently specified and ... Caveat: The ESM load hook and namespaced exports from CommonJS modules...
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 >Pure ESM package - gists · GitHub
ESM is natively supported by Node.js 12 and later. My repos are not the place to ask ESM/TypeScript/Webpack/Jest/ts-node/CRA support questions.
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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Just adding my +1 to this as it’s currently preventing me using ES modules in Cloud Functions (not firebase). I’d be happy to contribute if that’s useful.
Hey folks, just wanted to provide a nudge and a backlink to https://github.com/firebase/firebase-tools/issues/2994 – on the Firebase side we have 50+ developers 👍 requesting ESM support. We can’t add it for Firebase until it gets added for FF, so if it’s possible to make progress that would be great for our users!