Skip to content

Commit c17087f

Browse files
authored
docs: add docs for esm support (#308)
- Add docs for using ESM - Removes duplicate doc for debugging functions Fixes #302
1 parent 73c81b8 commit c17087f

File tree

2 files changed

+59
-33
lines changed

2 files changed

+59
-33
lines changed

docs/README.md

+2-33
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,8 @@ This directory contains advanced docs around the Functions Framework.
66
- [Debugging Functions](debugging.md)
77
- [Running and Deploying Docker Containers](docker.md)
88
- [Writing a Function in Typescript](typescript.md)
9+
- [ES Modules](esm.md)
910

1011
## TODO Docs
1112

12-
- TODO: Run Multiple Cloud Functions [#23](https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/23)
13-
14-
## Debugging functions
15-
16-
The Functions Framework works with standard tooling that you might use when writing a function for a Node.js environment. You can attach a debugger to your function by following these steps.
17-
18-
1. Write an `index.js` file containing your Node.js function:
19-
20-
```js
21-
exports.helloWorld = (req, res) => {
22-
res.send('Hello, World');
23-
};
24-
```
25-
26-
2. Install the Functions Framework:
27-
28-
```sh
29-
npm install @google-cloud/functions-framework
30-
```
31-
32-
3. Run `node`, enable the inspector and run the Functions Framework:
33-
34-
```sh
35-
node --inspect node_modules/@google-cloud/functions-framework --target=helloWorld
36-
...
37-
Debugger listening on ws://127.0.0.1:9229/5f57f5e9-ea4b-43ce-be1d-6e9b838ade4a
38-
For help see https://nodejs.org/en/docs/inspector
39-
Serving function...
40-
Function: helloWorld
41-
URL: http://localhost:8080/
42-
```
43-
44-
You can now use an IDE or other tooling to add breakpoints, step through your code and debug your function.
13+
- Run Multiple Cloud Functions [#23](https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/23)

docs/esm.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Using ES Modules
2+
3+
The Functions Framework >= `1.9.0` supports loading your code as an ES Module.
4+
5+
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.
6+
7+
## Example
8+
9+
Before:
10+
11+
```js
12+
exports.helloGET = (req, res) => {
13+
res.send('No ESM.');
14+
};
15+
```
16+
17+
After:
18+
19+
```js
20+
export const helloGET = (req, res) => {
21+
res.send('ESM!');
22+
};
23+
```
24+
25+
## Quickstart
26+
27+
Create a `package.json` file:
28+
29+
```json
30+
{
31+
"type": "module",
32+
"scripts": {
33+
"start": "functions-framework --target=helloGET"
34+
},
35+
"main": "index.js",
36+
"dependencies": {
37+
"@google-cloud/functions-framework": "^1.9.0"
38+
}
39+
}
40+
```
41+
42+
Create a `index.js` file:
43+
44+
```js
45+
export const helloGET = (req, res) => {
46+
res.send('ESM!');
47+
};
48+
```
49+
50+
Install dependencies and start the framework:
51+
52+
```sh
53+
npm i
54+
npm start
55+
```
56+
57+
Go to `localhost:8080/` and see your function execute!

0 commit comments

Comments
 (0)