I am trying to deploy an ESM function like this:
export const handler = async (event: any, context: any) => {
return {
statusCode: 200,
body: 'Hello, World!'
}
}
which, during build, compiles to javascript ES module:
export const handler = async (event, context) => {
return {
statusCode: 200,
body: 'Hello, World!'
};
};
Here is how my netlify.toml
looks like:
[build]
publish = "public"
functions = "functions"
command = "npm run build"
environment = { NODE_VERSION = "15.5.0", AWS_LAMBDA_JS_RUNTIME = "15.5.0" }
[build.processing]
skip_processing = true
When I call the function .netlify/functions/foo
, an error occurs saying Unexpected token 'export'
:
{
"errorType": "Runtime.UserCodeSyntaxError",
"errorMessage": "SyntaxError: Unexpected token 'export'",
"trace": [
"Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token 'export'",
" at _loadUserApp (/var/runtime/UserFunction.js:98:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:999:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
" at Module.load (internal/modules/cjs/loader.js:863:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
" at internal/main/run_main_module.js:17:47"
]
}
I tried renaming foo.js
to foo.mjs
, but it only results in No Functions were found
.
It looks like that node.js does not consider foo.js
an ES module, which is the expected behavior. And netlify does not consider foo.mjs
a lambda function, which is also expected.
However, this behavior makes it seemingly impossible to deploy ES module labmda functions. How should I work it out ?