本文介绍了如何在 ES6 语法中导入 firebase-functions 和 firebase-admin 以使用 Babel for Node 10 进行转译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用 ES6 编写我的云函数,并使用 Babel 进行编译以针对 Node v10 环境.而且我注意到了一些奇怪的事情.

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.

为什么当我像这样导入 firebase-functions 时:

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

从firebase-functions"导入函数;

我收到此错误:

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

要修复它,我需要像这样导入它:

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

import * as functions from 'firebase-functions';

虽然以下 import 适用于 firebase-admin:

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

从firebase-admin"导入管理员;

问题

简而言之,问题是:

为什么会这样:

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

推荐答案

import functions from 'firebase-functions'; 不起作用的原因是因为'firebase-functions' 没有functions"默认导出.

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

因此,这个错误:

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

解决方案:

第一个选项是导入整个模块的内容并将 functions 添加到当前范围,其中包含模块 firebase-functions 的所有导出.


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'

第二个选项是从模块导入单个导出,在这种情况下是 https,因为您正在尝试读取 'firebase 的属性 https-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'

更多信息可以在这里找到.

希望这可以澄清您的问题.

Hope this clarifies your question.

这篇关于如何在 ES6 语法中导入 firebase-functions 和 firebase-admin 以使用 Babel for Node 10 进行转译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 08:05