Firebase Cloud函数和Nuxt3:不能在模块外使用import语句



仅供参考:我尝试过这里的解决方案,但没有成功

我正在尝试将我的firebase云功能组织到单独的文件中。我现在只有一个函数导出文件作为测试。

我的根目录中有一个名为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应用程序,所以当我运行build命令(npm run build(时,它会将整个应用程序代码(包括functions文件夹(编译到一个文件夹中进行分发。这似乎很有效。

然而,在构建阶段之后,我运行firebase emulators:start并得到以下错误:

PS C:UsersXXXDesktopworkXXX> 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:UsersXXXDesktopworkXXX.outputserver" for Cloud Functions...
+  functions[us-central1-server]: http function initialized (http://localhost:5001/XXX/us-central1/server).
i  functions: Watching "C:UsersXXXDesktopworkXXXfunctions" 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:UsersXXXDesktopworkXXX> npm run build

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

"type": "module"

但是,我仍然得到上面的错误!有什么办法吗

我在组织函数时也遇到了这样的问题。我必须用户要求才能使其正常工作。

出口。[groupname]=require('<函数路径.ts>'(;

然后,该函数被重命名为groupname functionnameexported。

例如:

删除:

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

替换为index.ts:中的以下内容

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

然后,您的功能将在测试helloWorld时可用,并可访问http://localhost:5001/[您的firebase项目]/us-central1/在firebase控制台指向您的任何位置测试helloWorld(如果正在模拟(。

相关内容

  • 没有找到相关文章

最新更新