有没有办法在Cloud Functions for Firebase中使用es6语法?
import functions from 'firebase-functions';
export const helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
})
失败:
SyntaxError: Unexpected token import
答案 0 :(得分:10)
简短的回答是,不,你不能这样做 - 但是。它与云功能没有任何关系。它与节点有关。如果使用命令node index.js
将节点指向index.js,则会看到完全相同的错误。
在一些博客中讨论了关于为什么这是一个复杂问题的长篇答案,例如here和here。
编辑:firebase CLI now supports projects using TypeScript,它允许您访问ES7语法。它会自动将其编译为ES6,以便部署到云功能。
答案 1 :(得分:6)
lst = lapply(lst, cbind, new_col = '')
> lst
[[1]]
a b new_col
1 1 66
2 1 999
3 23 89
[[2]]
a b new_col
1 99 61
2 32 99
3 83 19
和import
等ES6功能,甚至是const
和await
等ES7功能,请使用async
重命名为index.js
index.ts
1}}。
这是我的index.ts
:
import * as functions from 'firebase-functions';
export const helloWorld = functions.https.onRequest((req, resp) => {
resp.send("Hello from Firebase!");
});
Atom也提示我生成functions/tsconfig.json
文件。我不确定这是否必要,但这是生成的内容:
{
"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
}
}
这是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
答案 2 :(得分:5)
可以通过完整步骤查看此链接:
<强>予。不推荐使用: https://codeburst.io/es6-in-cloud-functions-for-firebase-959b35e31cb0
创建./functionsES6
并将package.json
,yarn.lock
和index.js
从./functions.
在./functionsES6/package.json
Yarn Deploy
<强> II。更新: https://codeburst.io/es6-in-cloud-functions-for-firebase-2-415d15205468
您可以在package.json
答案 3 :(得分:5)
Firebase CLI 现在支持 node.js 14,但这并不意味着可以将 Cloud Functions 编写为 ES 模块。
缺失的部分是:
firebase-functions
(3.13.1) npm 模块不提供 ES 导出
Firebase 模拟器 (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.
Meta:虽然建议的方式(esm
包;转换为 TypeScript)可能适用于某些人,但我个人只对纯 ES 模块功能感兴趣。这也符合问题的标题(“... using es6 import statement”),所以我决定写一个总结当前(2021年1月)的情况。
能够将 Cloud Functions 编码为 ES 模块并没有直接的回报。这只是一个语法问题:如果我用它们编写我的网络应用程序,并且 node 支持它们,我自然更愿意将它们也用于 Cloud Functions。
如果您喜欢,请here is the ticket关注/?。
答案 4 :(得分:0)
使用纯JS导入库也会修复它。
示例:
var _ = require('lodash');
而不是:
import _ from 'lodash';
答案 5 :(得分:0)
简单方法是使用esm packahe https://www.npmjs.com/package/esm
require = require("esm")(module /*, options*/ )