Closed
Description
ESM support stopped working today in Google Cloud Functions (GCF) environment with Node.js 16 runtime. Trying to deploy an ESM codebase fails with the error:
To load an ES module, set "type": "module" in the package.
package.json
{
"name": "esm",
"type": "module",
"exports": "./index.js",
"packageManager": "[email protected]"
}
index.js
export function esm(req, res) {
res.send("PASS");
}
$ cloud functions deploy esm \
--project=example --region=us-central1 --allow-unauthenticated --trigger-http \
--entry-point=esm --memory=256MB --runtime=nodejs16 --source=. \
--set-env-vars="NODE_OPTIONS=--loader=./.pnp.loader.mjs --require=./.pnp.cjs --no-warnings"
Workaround (if you want to preserve ESM in your project)
- Bundle the code. E.g. by using Rollup and Babel wtih
@babel-preset/env
,target: { node: "14" }
preset - Deploy to Node.js v14 GCF runtime. See example: GraphQL API and Relay Starter Kit →
api/rollup.config.js