我對“寫出檔案想要的東西,但結果卻不同”而發瘋。
我正在嘗試將 firebase 匯入我的 express.js API,以便應用程式可以直接從資料庫中獲取。
// Initialize Firebase
const functions = require('firebase-functions');
import { initializeApp } from 'firebase/app';
const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);
... //some other code
我在 https://firebase.google.com/docs/web/setup 和 SyntaxError: Cannot use import statement outside a module Firebase Functions尋找解決方案
但似乎問題完全不同。結果仍然是“語法錯誤:無法在模塊外使用匯入陳述句。”
誰能解釋發生了什么?我錯過了什么?
uj5u.com熱心網友回復:
最新的 Firebase 版本現在使用 ES6/模塊化語法,這反過來會導致SyntaxError: Cannot use import statement outside a module.你必須"type": "module"在你的package.json. 這啟用了 ES6 模塊發現。此外,使用“模塊”時,您還必須將所有require語法更改為import語法。這是您的示例代碼供參考:
import * as functions from "firebase-functions";
import { initializeApp } from 'firebase/app';
const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);
包.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"type": "module",
...
}
有關更多資訊,您可以查看以下檔案:
- ECMAScript 模塊
- 從版本 8 升級到模塊化 Web SDK
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/512375.html
標籤:Google Cloud Collective 节点.js火力基地api休息表示
上一篇:將Spark資料框單元格內容與PySpark中的列值匹配
下一篇:在axios呼叫中獲取未定義的值
