How to import firebase-functions and firebase-admin in ES6 syntax for transpiling with Babel for Node 10

回眸只為那壹抹淺笑 提交于 2021-02-08 19:02:07

问题


I'm currently writing my cloud functions in ES6 and transpiling with Babel to target the Node v10 environment. And I've noticed something weird.

Why is that when I import firebase-functions like this:

import functions from 'firebase-functions';

I get this error:

!  TypeError: Cannot read property 'https' of undefined
    at Object.<anonymous> (C:\myProject\functions\index.js:28:55)

And to fix it, I need to import it like this:

import * as functions from 'firebase-functions';

While the following import works just fine for the firebase-admin:

import admin from 'firebase-admin';

QUESTION

In short terms, the question is:

Why is that:

import functions from 'firebase-functions';            // DOESN'T WORK
import * as functions from 'firebase-functions';       // WORKS
import admin from 'firebase-admin';                    // WORKS

回答1:


The reason why import functions from 'firebase-functions'; will not work is because 'firebase-functions' does not have a "functions" default export.

Hence, this error:

!  TypeError: Cannot read property 'https' of undefined
    at Object.<anonymous> (C:\myProject\functions\index.js:28:55)

Solution:

The first option would be to import an entire module's contents and add functions into the current scope containing all the exports from the module firebase-functions.

import * as functions from 'firebase-functions'

The second option would be to import a single export from a module, https in this case, since you are trying to read property https of 'firebase-functions'.

import { https } from 'firebase-functions'

More information can be found here.

Hope this clarifies your question.



来源:https://stackoverflow.com/questions/58817431/how-to-import-firebase-functions-and-firebase-admin-in-es6-syntax-for-transpilin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!