'Cloud Functions for Firebase using es6 import statement

Is there any way to use es6 syntax in Cloud Functions for Firebase?

import functions from 'firebase-functions';

export const helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!");
})

fails on:

SyntaxError: Unexpected token import


Solution 1:[1]

The short answer is, no, you can't do this - yet. It doesn't have anything to do with Cloud Functions. It has to do with node. If you point node at your index.js with the command node index.js, you'll see the exact same error.

The long answer about why this is a complicated problem has been discussed in some blogs, for example here and here.

EDIT: The firebase CLI now supports projects using TypeScript, which gives you access to ES7 syntax. It will automatically compile that down to ES6 for deployment to Cloud Functions.

Solution 2:[2]

Ahh figured it out. To use ES6 features like import and const, and even ES7 features like await and async, use Typescript by renaming index.js to index.ts.

Here's my index.ts:

import * as functions from 'firebase-functions';

export const helloWorld = functions.https.onRequest((req, resp) => {
 resp.send("Hello from Firebase!");
});

I was also prompted by Atom to generate a functions/tsconfig.json file. I'm not sure this was necessary, but here's what was generated:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "isolatedModules": false,
        "jsx": "react",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "declaration": false,
        "noImplicitAny": false,
        "noImplicitUseStrict": false,
        "removeComments": true,
        "noLib": false,
        "preserveConstEnums": true,
        "suppressImplicitAnyIndexErrors": true,
        "lib": ["es2015", "es2015.promise"]
    },
    "exclude": [
        "node_modules",
        "typings/browser",
        "typings/browser.d.ts"
    ],
    "compileOnSave": true,
    "buildOnSave": false,
    "atom": {
        "rewriteTsconfig": false
    }
}

Here's the output generated by firebase deploy --only functions:

=== Deploying to 'PROJECTNAME'...

i  deploying functions
i  functions: ensuring necessary APIs are enabled...
i  runtimeconfig: ensuring necessary APIs are enabled...
?  runtimeconfig: all necessary APIs are enabled
?  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (1.53 KB) for uploading
?  functions: functions folder uploaded successfully
i  starting release process (may take several minutes)...
i  functions: creating function helloWorld...
?  functions[helloWorld]: Successful create operation.
?  functions: all functions deployed successfully!

?  Deploy complete!

Project Console: https://console.firebase.google.com/project/PROJECTNAME/overview
Function URL (helloWorld): https://us-central1-PROJECTNAME.cloudfunctions.net/helloWorld

Solution 3:[3]

Firebase CLI now supports node.js 14, but it does not mean one can write Cloud Functions as ES modules.

Missing pieces are:

  1. firebase-functions (3.13.1) npm module does not offer ES exports

  2. Firebase emulator (firebase-tools 9.2.2) does not load Cloud Functions that are ES modules:

    [emul] require() of /Users/.../functions/index.js from /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
    

Meta: While the suggested ways (esm package; converting to TypeScript) may work for some, I'm personally interested only in pure ES module functionality. This is also in line with the title of the question ("... using es6 import statement"), so I decided to write a summary of current (Jan 2021) situation.

There is no direct payback from being able to code Cloud Functions as ES modules. It's just a syntactical thing: if I write my web app in them, and node supports them, naturally I would prefer using them also for Cloud Functions.


If it's dear to you, here is the ticket to follow/?.

Solution 4:[4]

It is possible check this Link with full steps :

I. Deprecated : https://codeburst.io/es6-in-cloud-functions-for-firebase-959b35e31cb0

  1. Create ./functionsES6 and copy the package.json, yarn.lock and index.js into it from ./functions.

  2. Add the presets in ./functionsES6/package.json

enter image description here

  1. While you are in root path ./functionsES6 - Yarn Deploy

enter image description here

II. Updated : https://codeburst.io/es6-in-cloud-functions-for-firebase-2-415d15205468

You can use these in package.json

enter image description here

Solution 5:[5]

Simple method is use esm packahe https://www.npmjs.com/package/esm

require = require("esm")(module /*, options*/ )

Solution 6:[6]

Using ES modules is now experimental, to use ESM within a Cloud Function, you must declare "type": "module" within your package.json.

  ...
  "type": "module",
  ...
}

Then you can use import and export statements.
Reference: https://cloud.google.com/functions/docs/concepts/nodejs-runtime#using_es_modules_experimental
Code example: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/master/docs/esm/README.md

Solution 7:[7]

File: component.ts

exports.methodName = fn() => {}


File: index.ts (used to call all exportable functions)

... export * from './component.ts'; ...

This must solve part of using es6 for export functions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 akauppi
Solution 4
Solution 5 PRAJIN PRAKASH
Solution 6 Roberto
Solution 7 edyrkaj