question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ESM support stopped working in Cloud Functions (GCF)

See original GitHub issue

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)

  1. Bundle the code. E.g. by using Rollup and Babel wtih @babel-preset/env, target: { node: "14" } preset
  2. Deploy to Node.js v14 GCF runtime. See example: GraphQL API and Relay Starter Kitapi/rollup.config.js

Ref #233, #292, https://github.com/nodejs/node/issues/41189, https://github.com/nodejs/node/pull/41198

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:11
  • Comments:28 (13 by maintainers)

github_iconTop GitHub Comments

8reactions
matthewrobertsoncommented, Dec 15, 2021

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.

6reactions
matthewrobertsoncommented, Feb 1, 2022

The rollout completed a few hours ago. I just confirmed that I can deploy an ESM function in europe-west1 with the nodejs16 runtime. Please re-open the bug if you are still having issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running Functions with Function Frameworks - Google Cloud
Once you're finished running your function, stop the running framework instance by pressing CTRL-C . Was this helpful? Send feedback.
Read more >
Can't use ES modules in Google Cloud Functions
Google Cloud Functions (and therefore Firebase Functions) does not support ESM modules yet:.
Read more >
Getting started with Cloud Functions (2nd gen)
In this codelab, you will learn about Google Cloud Functions (2nd gen). More specifically, you will deploy functions that respond to HTTP ...
Read more >
ES Modules in Cloud Functions - Medium
ES modules (aka ECMAScript modules or ESM) are a TC39 standard, ... With recent updates, Google Cloud Functions for Node now supports ES ......
Read more >
graphql-upload-minimal - npm
Google Cloud Functions (GCF) Experimental. Untested. Azure Functions Working. Koa · Express.js. See also GraphQL multipart request spec server ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found