Cloud Functions deployment fails: "Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded."

Doug Stevenson picture Doug Stevenson · Jun 13, 2019 · Viewed 8.8k times · Source

I upgraded my firebase-functions module to 3.0.1. Now, when I deploy Cloud Functions, I get the warning message:

⚠ functions: Deploying functions to Node 6 runtime, which is deprecated. Node 8 is available and is the recommended runtime.

Then, deployment fails with this:

Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/node_modules/firebase-functions/lib/providers/https.js:282
    const func = async (req, res) => {
                       ^

SyntaxError: Unexpected token (
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/index.js:39:15)

How can I fix this?

Answer

Doug Stevenson picture Doug Stevenson · Jun 13, 2019

In the past, node 6 was the default target runtime. Now, node 6 has expired LTS (Long Term Support). With CLI version 6.8.0, node 6 was deprecated, and you were encouraged to target node 8 for deployment instead. Now, starting with [email protected], node 6 support is fully removed, and you must explicitly target node 8 in your package.json:

{
  // other configurations here…
  "dependencies": {
  },
  // Add an “engines” child to choose a node version, here it’s node 8.
  "engines": {
    "node": "8"
  }
}

Another related change in this version is a dependency on firebase-admin 8.x, which also drops support for node 6.

The error message itself is indicating that the keyword async isn't recognized, which is not supported by node 6.