首页 > 解决方案 > 在 Firebase 云函数中使用导入

问题描述

我正在尝试在"serve": "firebase emulators:start --only functions"具有节点版本 12 的本地计算机上运行 firebase 云功能。
我不能使用.mjs扩展,因为如果我使用 .mjs,firebase 将无法识别它并给我这个错误:

Error: Cannot find module '/home/rumesh/IdeaProjects/firebaseapp/functions'

不能"type":module在 package.json 中使用给我这个错误:

require() of ES modules is not supported.
require() of /home/rumesh/IdeaProjects/firebaseapp/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.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/rumesh/IdeaProjects/firebaseapp/functions/package.json. 

在这种情况下,如何在不使用 require 语句的情况下使用 import 语句?

标签: javascriptnode.jsfirebasegoogle-cloud-functions

解决方案


推荐阅读