簡體   English   中英

Firebase Cloud Functions 和 Nuxt3:無法在模塊外使用 import 語句

[英]Firebase Cloud Functions and Nuxt3: Cannot use import statement outside a module

僅供參考:我已經嘗試過這里的解決方案,但沒有奏效。

我正在嘗試將我的 firebase 雲功能組織到單獨的文件中。 我現在只有一個 function 導出文件作為測試。

我的根目錄中有一個名為functions的文件夾,其中包含一個名為helloWorld.ts的文件和一個index.ts文件:

/functions/helloWorld.ts

import * as functions from 'firebase-functions'

export const helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info('Hello logs!', { structuredData: true })
  response.send('Hello from Firebase!')
})

/functions/index.ts

// import all functions as needed
import { helloWorld } from './helloWorld'

export { helloWorld }

這是一個 Nuxt 3 應用程序,因此當我運行構建命令 ( npm run build ) 時,它會將整個應用程序代碼(包括functions文件夾)編譯到一個文件夾中以進行分發。 這似乎工作正常。

但是,在構建階段之后,我運行firebase emulators:start並得到以下錯誤:

PS C:\Users\XXX\Desktop\work\XXX> firebase emulators:start
i  emulators: Starting emulators: functions, hosting
!  functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, firestore, database, pubsub, storage
+  functions: Using node@16 from host.
+  functions: Using node@16 from host.
i  hosting[XXX]: Serving hosting files from: .output/public
+  hosting[XXX]: Local server: http://localhost:5000
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "C:\Users\XXX\Desktop\work\XXX\.output\server" for Cloud Functions...
+  functions[us-central1-server]: http function initialized (http://localhost:5001/XXX/us-central1/server).
i  functions: Watching "C:\Users\XXX\Desktop\work\XXX\functions" for Cloud Functions...
i  emulators: Shutting down emulators.
i  ui: Stopping Emulator UI
!  Emulator UI has exited upon receiving signal: SIGINT
i  hosting: Stopping Hosting Emulator
i  hub: Stopping emulator hub
i  logging: Stopping Logging Emulator

Error: Failed to load function definition from source: Failed to generate manifest from function source: SyntaxError: Cannot use import statement outside a module
PS C:\Users\XXX\Desktop\work\XXX> npm run build

因此,我編輯了functions/package.json文件以包括:

"type": "module"

但是,我仍然得到上述錯誤? 任何想法如何解決?

我也遇到了這樣的問題,試圖組織我的功能。 我必須用戶要求才能使其正常工作。

exports.[groupname] = require('<path-to-function.ts>');

然后 function 被重命名為 groupname-functionnameexported。

對於您的示例,它將是:

消除:

// import all functions as needed
import { helloWorld } from './helloWorld'

export { helloWorld }

在 index.ts 中替換為以下內容:

exports.testing = require('./helloWorld');

Then your function will be available as testing-helloWorld and accessible at http://localhost:5001/[your-firebase-project]/us-central1/testing-helloWorld (if emulating) otherwise at wherever the Firebase console points you.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM