Skip to content

Latest commit

 

History

History
64 lines (47 loc) · 1.31 KB

README.md

File metadata and controls

64 lines (47 loc) · 1.31 KB

Using ES Modules

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.

Example

Before:

exports.helloGET = (req, res) => {
 res.send('No ESM.');
};

After:

export const helloGET = (req, res) => {
 res.send('ESM!');
};

Quickstart

Google Cloud Platform logo

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-only

Create a index.js file:

export const helloGET = (req, res) => {
  res.send('ESM!');
};

Install dependencies and start the framework:

npm i
npm start

Go to localhost:8080/ and see your function execute!