Cloud functions for Firebase using es6 import statement

Can es6 syntax be used in cloud functions for Firebase?

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

does not work:

 SyntaxError: Unexpected token import 
+5
source share
4 answers

Short answer: no, you cannot do this. This has nothing to do with cloud features. This is due to node. If you point node to your index.js using the node index.js , you will see the same error.

The long answer about why this is a complex issue has been discussed on some blogs, like here and here .

EDIT: CLI Firebase now supports TypeScript projects , giving you access to ES7 syntax. It will automatically compile this to ES6 for deployment in cloud features.

+8
source

Ah realized that. To use ES6 features like import and const , and even ES7 features like await and async , use Typescript to rename index.js to index.ts .

Here is 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 asked by Atom to create the functions/tsconfig.json . I'm not sure if this was necessary, but here is what was created:

 { "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 is 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 
+4
source

You can check this link with complete steps:

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

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

  • Add presets to ./functionsES6/package.json

enter image description here

  1. While you are in the root path. / functionsES 6 - Yarn Deploy

enter image description here

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

You can use them in package.json

enter image description here

+3
source

Using pure JS to import the library will also fix it.

Example:

 var _ = require('lodash'); 

instead:

 import _ from 'lodash'; 
-1
source

Source: https://habr.com/ru/post/1265331/


All Articles