node.js - Firebase 函数模拟器错误:找不到模块“../service-account.json”
问题描述
我正在尝试使用 firebase emulators: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
service-account.json 应该从 shared.ts 文件中访问:
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.
我的 tsconfig.json 现在在左大括号上显示错误:
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'
我在 find 时遇到错误index.js
,我通过更正文件中的错误来修复index.jspackage.json
的路径"main"
我仍然无法从模拟器运行我的函数 - 我发现解决方案是在我的项目的函数目录中运行以下命令。
firebase functions:config:get > .runtimeconfig.json
(在此处查看此答案:https ://stackoverflow.com/a/42978220/15046746 )
现在,当我运行时firebase emulators:start
,我的所有功能都已正确初始化,并且我可以运行emulators:exec
以运行模拟器中的所有测试功能。
推荐阅读
- testing - 带有网页界面的 SoupUI
- python - 如何将数据框转换为带有列表键的字典?
- python - 用于 mWebSocket 服务器的 Python WebSocket 客户端
- eclipse-plugin - 如何在 Eclipse 中将 checkstyle 降级到 8.40 或 8.41
- powershell - 我应该如何在我的 Windows Server 中创建一个防火墙表列表来显示端口 443 是否被阻止?
- javascript - 在运行 indented if block 之前防止单击两次
- shell - tmux中的`Ctrl b + d`和`Ctrl b + Ctrl z`有什么区别?
- javascript - 在 JSX 中为字符串添加选项卡
- amazon-cloudfront - 我可以在 cloudfront 中使用多个 index.html 吗?
- sql - JOIN 子句查找不重复的相关记录,导航外键链