如何解决Firebase 函数模拟器错误:找不到模块“../service-account.json”
我正在尝试使用 firebase emulator:exec 执行测试函数,模拟器成功启动但由于以下错误而无法加载函数代码
Error: Cannot find module '../service-account.json'
我已经多次通过 npm install、npm run-script lint、npm run-script build 等,也擦除了我的 node_modules 文件夹并再次运行 npm install,以及重新启动 vscode,但我反复收到错误。
我有一个函数文件夹,service-account.json 位于其中:
├─ firebase.json
├─ firestore.indexes.json
├─ firestore.rules
├─ functions
│ ├─ .eslintrc.json
│ ├─ .gitignore
│ ├─ firestore-debug.log
│ ├─ lib
│ ├─ node_modules
│ ├─ package-lock.json
│ ├─ package.json
│ ├─ pubsub-debug.log
│ ├─ src
│ │ ├─ controller
│ │ │ ├─ functions
│ │ │ └─ shared.ts
│ │ ├─ index.ts
│ │ ├─ model
│ │ ├─ play-billing
│ │ ├─ service-account-firebase.json
│ │ ├─ service-account.json
│ │ └─ typings.d.ts
│ ├─ test
│ ├─ tsconfig.dev.json
│ └─ tsconfig.json
├─ gradle
应该从 shared.ts 文件访问 service-account.json:
import * as functions from "firebase-functions";
import { PlayBilling } from "../play-billing";
import * as serviceAccountPlay from "../service-account.json";
import { InstanceIdManager } from "../model/InstanceIdManager";
import { ContentManager } from "../model/ContentManager";
/*
* This file defines shared resources that are used in functions
*/
// Shared config
export const PACKAGE_NAME = functions.config().app.package_name;
...
我已经通过将鼠标悬停在文件路径上来仔细检查文件路径是否正确,并且它正确显示了完整路径。 我已经检查了我的依赖项,一切似乎都已就位:
├── @types/chai@4.2.15
├── @types/mocha@8.2.1
├── @types/node@14.14.31
├── @typescript-eslint/eslint-plugin@4.15.1
├── @typescript-eslint/parser@4.15.1
├── chai@4.3.0
├── copyfiles@2.4.1
├── eslint-plugin-import@2.22.1
├── eslint-plugin-promise@4.3.1
├── eslint@7.20.0
├── express@4.17.1
├── firebase-admin@9.5.0
├── firebase-functions-test@0.2.3
├── firebase-functions@3.13.1
├── googleapis@67.1.0
├── mocha@8.3.0
├── ts-node@9.1.1
└── typescript@4.1.5
我特别困惑,因为我在另一个项目中有完全相同的结构、依赖项和功能代码,它在那里工作正常!
我错过了什么? 我已经搜索了其他答案,但找不到任何我尚未检查过的内容。
请注意,service-account.json 与我在另一个项目中使用的完全相同,我可以毫无问题地模拟测试功能。
更新 删除我的 lib 文件夹并重建它后,我现在在加载我的函数代码的模拟器时遇到了不同的错误:
It looks like you're trying to access functions.config().app but there is no value there.
Cannot find type deFinition file for 'express-serve-static-core 2'
但我看不出有什么问题。
{
"compilerOptions": {
"lib": [
"es6","ESNext.AsyncIterable"
],"module": "commonjs","noImplicitReturns": true,//"noUnusedLocals": true,"outDir": "lib","sourceMap": true,//"strict": true,"target": "es6","resolveJsonModule": true,"esModuleInterop": true
},"compileOnSave": true,"include": [
"src","test","./typings.d.ts",]
}
我的lib文件结构如下:
.
├── src
│ ├── controller
│ ├── index.js
│ ├── index.js.map
│ ├── model
│ ├── play-billing
│ ├── service-account-firebase.json
│ └── service-account.json
└── test
└── play-billing
解决方法
我最终按如下方式解决了所有问题:
首先,我删除了整个 lib
文件夹,然后运行
npm run-script build
构建一个新的 lib 文件夹。
修复了模拟器查找模块'...service-account.json'
的错误
我在查找 index.js
时出错,我通过更正 package.json
文件中的错误修复了该错误,用于路径 "main"
到 index.js
我仍然无法从模拟器运行我的函数 - 我发现解决方案是在我的项目的函数目录中运行以下命令。
firebase functions:config:get > .runtimeconfig.json
(在此处查看此答案:https://stackoverflow.com/a/42978220/15046746)
现在,当我运行 firebase emulators:start
时,我的所有函数都已正确初始化,而且我还可以运行 emulators:exec
来运行模拟器中的所有测试函数。
如果我读的对,您的服务帐户文件位于 src/
目录中。但是一旦您的函数构建完成,编译后的代码就会部署到 lib/
目录中。您的相对路径不再有效。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。