[英]Cannot find module 'firebase-admin' from 'index.cjs.js'
I was using @firebase/testing
on Jest, but since it's deprecated and instructed to use new one , I decided to move to @firebase/rules-unit-testing
.我在 Jest 上使用
@firebase/testing
,但由于它已被弃用并指示使用 new one ,我决定转向@firebase/rules-unit-testing
。
Here is the code where I just switched them.这是我刚刚切换它们的代码。
import { initializeAdminApp } from "@firebase/rules-unit-testing";
import "babel-polyfill";
it("is ok", async () => {
const admin = initializeAdminApp({ projectId: "my-project" });
try {
const doc = admin.firestore().collection("items").doc("item-1");
await doc.set({ name: "Item 1" });
const ss = await doc.get();
expect(ss.data()?.name).toBe("Item 1");
} finally {
await admin.delete();
}
});
When I run this test with emu, it results "Cannot find module 'firebase-admin' from 'index.cjs.js'" error.当我用emu运行这个测试时,它会导致“无法从'index.cjs.js'中找到模块'firebase-admin'”错误。
Cannot find module 'firebase-admin' from 'index.cjs.js'
3 |
4 | it("is ok", async () => {
> 5 | const admin = initializeAdminApp({ projectId: "my-project" });
| ^
It passes if it is the old @firebase/testing
.如果它是旧的
@firebase/testing
它就会通过。
What did I miss?我错过了什么?
The firebase-admin
is an npm package. firebase-admin
是一个 npm 包。 Just installed it and all worked.刚刚安装它并且一切正常。
$ npm install -D firebase-admin
I started to use firebase-admin@^10.0.1 and I had this error when running jest tests.我开始使用firebase-admin@^10.0.1并且在运行jest测试时出现此错误。 It couldn't map "firebase-admin/app" to "firebase-admin/lib/app" as expected.
它无法按预期将“firebase-admin/app”映射到“firebase-admin/lib/app”。 So I have mapped this manually on jest.config.ts :
所以我在jest.config.ts 上手动映射了这个:
import { pathsToModuleNameMapper } from 'ts-jest/utils';
import { compilerOptions } from './tsconfig.json';
...
export default {
...
moduleNameMapper: pathsToModuleNameMapper(
{
...compilerOptions.paths,
'firebase-admin/*': ['node_modules/firebase-admin/lib/*'],
},
{
prefix: '<rootDir>',
},
),
...
}
That worked for me.这对我有用。
And just to note, my tsconfig.json is like this:请注意,我的tsconfig.json是这样的:
{
"compilerOptions": {
...
"baseUrl": ".",
"paths": {
"modules/*": [
"src/modules/*"
],
"shared/*": [
"src/shared/*"
],
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.