错误 TS2307:找不到模块“@adminjs/mongoose”或其相应的类型声明。 mongoose 数据库与 AdminJS 集成时出错

问题描述 投票:0回答:1

美好的一天,我正在尝试使用 Express.js 将 mongoose 数据库集成到我的 AdminJS 项目中。我已经导入了所有必需的包,但是当我运行服务器时,我收到一条错误消息,指出找不到模块@adminjs/mongoose。我不确定这是否是兼容性问题,但我已尝试多次卸载并重新安装该软件包,但错误仍然存在。如果有人可以帮助我解决这个问题,我将不胜感激。以下是项目代码和错误消息:

主文件(app.ts): (Mongoose URL 存储在 .env 文件中):

import express from 'express';
import AdminJS from 'adminjs';
import AdminJSExpress from '@adminjs/express';
import mongoose from 'mongoose';
import * as AdminJSMongoose from '@adminjs/mongoose';
import * as dotenv from 'dotenv';
import { PersonModel } from './models/personModel.js';
dotenv.config();

const port = process.env.PORT || 3000;

AdminJS.registerAdapter({ 
    Resource: AdminJSMongoose.Resource,
    Database: AdminJSMongoose.Database,
});

const start = async (): Promise<void> => {
  const app = express();

  await mongoose.connect(`${process.env.MONGO_URL}`);

  const admin = new AdminJS({
    resources: [
      {
        resource: PersonModel
      }
    ]
  });

  const adminRouter = AdminJSExpress.buildRouter(admin);

  app.use(admin.options.rootPath, adminRouter);

  console.log(`Starting server on port ${port}`);
  app.listen(port, () => {
    console.log(`AdminJS available at http://localhost:${port}${admin.options.rootPath}`);
  });
};

start();

模型架构文件:

import { model, Schema } from 'mongoose';

export interface Person {
    name: string;
    age: number
};

export const PersonSchema = new Schema<Person>({
    name: { type: String, required: true },
    age: { type: Number, required: true }
});

export const PersonModel = model<Person>('Person', PersonSchema);

package.json 显示已安装的软件包:

{
  "name": "admin_wellfit",
  "version": "1.0.0",
  "description": "admin for wellfit project",
  "main": "./app.ts",
  "author": "OF",
  "license": "MIT",
  "devDependencies": {
    "@types/express": "^5.0.0",
    "ts-node": "^10.9.2",
    "tslib": "^2.7.0",
    "typescript": "^5.6.2"
  },
  "dependencies": {
    "@adminjs/express": "^6.1.0",
    "@adminjs/mongoose": "^3.0.1",
    "adminjs": "^7.8.13",
    "dotenv": "^16.4.5",
    "express": "^4.21.0",
    "express-formidable": "^1.2.0",
    "express-session": "^1.18.0",
    "mongoose": "^8.6.3"
  },
  "scripts": {
    "dev": "ts-node app.ts"
  }
}

运行服务器时出现错误消息:

TSError: ⨯ Unable to compile TypeScript:
app.ts:5:34 - error TS2307: Cannot find module '@adminjs/mongoose' or its corresponding type declarations.

import * as AdminJSMongoose from '@adminjs/mongoose';

错误截图

我尝试按照文档将

@adminjs/mongoose
包安装并导入到我的 AdminJS 项目中,以将 Mongoose 数据库与 AdminJS 集成。我希望该模块能够被识别并允许我在 AdminJS 中注册 Mongoose 适配器,但相反,我收到了 TypeScript 错误,表明无法找到该模块或其类型声明丢失。我多次尝试重新安装该软件包,但问题仍然存在。

node.js typescript express mongoose adminjs
1个回答
0
投票

我找到了解决方案,这是与 @adminjs/mongoose 的兼容性错误,我必须将 tsconfig.json 存档中的模块版本更改为“Node16”,它对我有用。

{
  "compilerOptions": {
    "target": "ES6",
    "module": "Node16",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "moduleResolution": "node16",
    "resolveJsonModule": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

© www.soinside.com 2019 - 2024. All rights reserved.