functions-framework-nodejs: ESM support stopped working in Cloud Functions (GCF)
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": "yarn@3.2.0-rc.12"
}
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
Ref #233, #292, https://github.com/nodejs/node/issues/41189, https://github.com/nodejs/node/pull/41198
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 11
- Comments: 28 (13 by maintainers)
Commits related to this issue
- Revert to node 14 https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/407 — committed to gdg-x/hoverboard by abraham 2 years ago
- Revert to node 14 https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/407 — committed to gdg-x/hoverboard by abraham 2 years ago
- Skip syntax error checking users functions using ESM This is a temporary workaround for a regression in Node.js 16 that causes `node --check` to return false positives when a project is using ESM. M... — committed to GoogleCloudPlatform/buildpacks by matthewrobertson 2 years ago
This appears to be a regression in v16 of the Node.js engine. I have filed https://github.com/nodejs/node/issues/41189. Unfortunately, the work around is to use
nodejs14
on GCF for now.The rollout completed a few hours ago. I just confirmed that I can deploy an ESM function in
europe-west1
with thenodejs16
runtime. Please re-open the bug if you are still having issues.The work around is rolling out now. It should be in all GCP regions by the end of the week.
I was really hoping that Node.js would have released a fix for this by now, but I just tested with
v16.13.2 (npm v8.1.2)
and it is still an issue. I will add a temporary workaround in the buildpacks.Is there any plans on resolving this soon? I’m using another package that requires Node 16 so going back to node 14 engine is not an option for me.
When can we expect these changes to be deployed to prod?
@matthewrobertson Is it possible to revert the version of node v16 used in the GCF runtime?
Understood. Matt is actively working on a solution that is visible internally https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/407#issuecomment-1017870928. There’s no update since yesterday.
The needed commit is already scheduled for backporting as indicated by the lts-watch label in https://github.com/nodejs/node/pull/41198.
We’ll have to wait until the 16 LTS backporter is ready to backport and cut a new release…
@matthewrobertson is there an ETA on when this will be fixed?