You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/views/docs/en/guides/developer-experience/using-esm.md
+8-9Lines changed: 8 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,17 +5,16 @@ description: How to use ECMAScript Modules in functions
5
5
---
6
6
7
7
[AWS Lambdas support ES modules and top-level await](https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/). Architect adheres to Node.js conventions and the AWS implementation.
8
-
Lambdas must use the `nodejs14.x` or greater runtime to leverage ESM.
9
8
10
9
## Example project
11
10
12
-
A working Architect project with each method for using ESM and CJS, can be found on GitHub: [`architect-examples/arc-example-esm-cjs`](https://github.com/architect-examples/arc-example-esm-cjs).
11
+
A working Architect project with each method for using ESM and CJS can be found on GitHub: [`architect-examples/arc-example-esm-cjs`](https://github.com/architect-examples/arc-example-esm-cjs).
13
12
14
-
## CommonJS by default
13
+
## ESM by default
15
14
16
-
Architect projects currently default to CommonJS. In the future, `arc init` and `arc create` may generate ESM functions by default. Architect will always support CJS so long as Node.js does.
15
+
Architect projects default to ESM. Architect will always support CJS so long as Node.js does.
17
16
18
-
## ES Modules with `.mjs`
17
+
## ESM with `.mjs`
19
18
20
19
The simplest way to start using ESM is to create JavaScript files with a `.mjs` extension. For example, no configuration is needed for the following shared helper and HTTP GET function to work as ES modules:
21
20
@@ -26,7 +25,7 @@ export default function () {
26
25
}
27
26
```
28
27
29
-
The shared ESM file can be imported in any function. Remember, ES module import statements require the file extension.
28
+
The shared ESM file can be imported in any function. Remember, ESM `import` statements require the file extension.
30
29
31
30
```javascript
32
31
// ./src/http/get-index/index.mjs
@@ -37,9 +36,9 @@ export async function handler (event) {
37
36
}
38
37
```
39
38
40
-
## ES Modules with `package.json`
39
+
## ESM with `package.json`
41
40
42
-
Alternatively, the `"type"` property of a function's package.json file can be set to `"module"` to declare the function is ESM. That function's handler file can then use a `.js` file extension:
41
+
Alternatively, if you are using [manual per-function dependency management](dependency-management#manual-dependency-management), the `"type"` property of a function's `package.json` file can be set to `"module"` to declare the function is ESM. That function's handler file can then use a `.js` file extension:
43
42
44
43
```json
45
44
// ./src/http/get-index/package.json
@@ -59,7 +58,7 @@ Alternatively, the `"type"` property of a function's package.json file can be se
59
58
└── app.arc
60
59
```
61
60
62
-
Declaring dependencies in a function's `package.json` will disable Lambda treeshaking for that function. This is true no matter the module type.
61
+
Declaring dependencies in a function's `package.json` will disable [automated dependency treeshaking](dependency-management#automated-dependency-treeshaking) for that function. This is true no matter the module type.
63
62
64
63
> ℹ️ Setting `"type": "module"` in the project's root `package.json` will not affect function module types.
0 commit comments