Skip to content

ESM support stopped working in Cloud Functions (GCF) #407

Closed
@koistya

Description

@koistya

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)

  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, nodejs/node#41189, nodejs/node#41198

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions