The Functions Framework >= 0.3.6 supports loading your code as an ES Module.
ECMAScript modules (ES modules or ESM) are a TC39 standard, unflagged feature in Node >=14 for loading JavaScript modules. As opposed to CommonJS, ESM provides an asynchronous API for loading modules and provides a very commonly adopted syntax improvement via import and export statements.
Before:
exports.helloGET = (req, res) => {
res.send('No ESM.');
};After:
export const helloGET = (req, res) => {
res.send('ESM!');
};Create a package.json file:
{
"type": "module",
"scripts": {
"start": "functions-framework --target=helloGET"
},
"main": "index.js",
"dependencies": {
"@openfunction/functions-framework": "^0.3.6"
}
}Update corresponding package-lock.json:
npm install @openfunction/functions-framework --package-lock-onlyCreate a index.js file:
export const helloGET = (req, res) => {
res.send('ESM!');
};Install dependencies and start the framework:
npm i
npm startGo to localhost:8080/ and see your function execute!