node.jstypescriptexpressmongooseadminjs

Error TS2307: Cannot find module '@adminjs/mongoose' or its corresponding type declarations. Error integrating mongoose database with AdminJS


Good day, I am trying to integrate a mongoose database into my AdminJS project using Express.js. I have imported all the necessary packages, but when I run the server, I get an error saying that the module @adminjs/mongoose cannot be found. I'm not sure if it's a compatibility issue, but I have tried uninstalling and reinstalling the package multiple times, and the error persists. If anyone can help me resolve this issue, I would greatly appreciate it. Below is the project code and the error messages:

Main file (app.ts): (The Mongoose URL is stored in a .env file):

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();

Model schema file:

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 showing the installed packages:

{
  "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"
  }
}

Error message when running the server:

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';

error screenshot

I tried to install and import the @adminjs/mongoose package into my AdminJS project, as per the documentation, to integrate a Mongoose database with AdminJS. I expected the module to be recognized and allow me to register the Mongoose adapter in AdminJS, but instead, I got a TypeScript error indicating that the module couldn't be found or its type declarations were missing. I tried reinstalling the package multiple times, but the issue persists.


Solution

  • I have found the solution, it was an compatibility error with @adminjs/mongoose, i had to change module version to "Node16" in the tsconfig.json archive and it worked for me.

    {
      "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"]
    }