使用es6导入语句的Firebase云函数
有没有办法在Firebase的云函数中使用es6语法使用es6导入语句的Firebase云函数,firebase,ecmascript-6,google-cloud-functions,Firebase,Ecmascript 6,Google Cloud Functions,有没有办法在Firebase的云函数中使用es6语法 import functions from 'firebase-functions'; export const helloWorld = functions.https.onRequest((request, response) => { response.send("Hello from Firebase!"); }) 失败于: SyntaxError: Unexpected token import 简单的回答是,不,你还
import functions from 'firebase-functions';
export const helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
})
失败于:
SyntaxError: Unexpected token import
简单的回答是,不,你还不能这么做。它与云功能没有任何关系。它与节点有关。如果使用命令
node index.js
将节点指向index.js,您将看到完全相同的错误
关于为什么这是一个复杂问题的详细答案已经在一些博客上讨论过,例如和
编辑:firebase CLI,允许您访问ES7语法。它将自动编译到ES6,以便部署到云功能。啊哈,我已经解决了这个问题。要使用ES6功能(如
import
和const
),甚至ES7功能(如wait
和async
),请将index.js
重命名为index.ts
)
这是我的索引。ts
:
import*作为“firebase函数”中的函数;
export const helloWorld=functions.https.onRequest((req,resp)=>{
响应发送(“来自Firebase的你好!”);
});
Atom还提示我生成一个functions/tsconfig.json
文件。我不确定这是否有必要,但以下是生成的内容:
{
“编译器选项”:{
“目标”:“es5”,
“模块”:“commonjs”,
“moduleResolution”:“节点”,
“隔离模块”:错误,
“jsx”:“反应”,
“实验生态学者”:没错,
“emit decoromentadata”:正确,
“声明”:虚假,
“noImplicitAny”:错,
“noImplicitUseStrict”:错误,
“removeComments”:正确,
“noLib”:错,
“保存常量枚举”:对,
“SuppressImplicationIndexErrors”:true,
“lib”:[“es2015”、“es2015.承诺”]
},
“排除”:[
“节点_模块”,
“打字/浏览”,
“打字/浏览器.d.ts”
],
“保存”:正确,
“buildOnSave”:false,
“原子”:{
“重写配置”:false
}
}
以下是由firebase部署生成的输出——仅限函数
:
=== 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
使用纯JS导入库也可以解决这个问题 例如:
var _ = require('lodash');
而不是:
import _ from 'lodash';
可以通过以下完整步骤检查此链接: I.已弃用:
/functions ses6
并将包.json
、纱线.lock
和索引.js
从/functions>复制到其中。
/functionsES6/package.json
package.json
简单的方法是使用esm软件包
Firebase CLI现在支持node.js 14,但这并不意味着可以将云函数编写为ES模块 缺失的部分包括:
firebase功能
(3.13.1)npm模块不提供ES导出
Firebase tools
9.2.2)不加载属于ES模块的云函数:
[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.
esm
package;转换为TypeScript)可能对某些人有用,但我个人只对纯ES模块功能感兴趣。这也符合问题的标题(“使用es6导入声明”),因此我决定写一份当前(2021年1月)情况的总结
将云函数编码为ES模块并没有直接的回报。这只是一个语法问题:如果我用它们编写web应用程序,并且node支持它们,自然我更愿意将它们也用于云功能
如果对你来说,跟随/但是他们在中做这件事是很重要的,我认为通过使用打字脚本。我只是不知道如何使用Typescript…Typescript只是转换成JavaScript。有一个编译程序
tsc
,它接受你的.ts文件并将它们转换成.js文件。这些JS文件实际上是部署到云功能中的文件。请参见下面我的答案-只需重命名文件即可(firebase cli显然知道typescript)。firebase cli不知道typescript。您所做的是将编辑器配置为从TypeScript自动生成JavaScript文件。这些生成的文件使您的函数实际上作为云函数运行。哦,您是对的。Atom会在保存时自动将Typescript编译为Javascript。请记住,如果您这样做,您就不再编写Javascript了。您正在编写TypeScript,并且您必须遵守该语言的规则,这是JS的超集,并且可能独立于JS发展。如果您正在编写TS,您不能简单地切换回JS并期望一切正常。当TS传输到JS时,您将看到它最终使用常规的旧JS require语句来实现导入。小注:云函数环境运行Node 6.9.1,它完全支持const
。您可以在@DougStevenson true查看其他受支持的功能。正在等待kotlin中的云功能。它已经是android的官方语言(比java更好),所以在kotlin中使用云功能在google环境中开发更无缝的体验是有意义的:)更不用说作为一名js开发人员需要我们像一个通灵师一样行事。我在重命名索引时遇到了这个错误错误:functions/index.js不存在,无法部署Firebase函数“@AlxVallejo首先在函数文件夹的cmd中运行tsc
,以生成索引。js问题不是如何修复错误。这是我们能否在谷歌云功能中使用es6的问题。这个答案比简单地说“不”要好。谢谢!这正是我要找的信息!
[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.