Search code examples
node.jstypescriptcompilationnestjsprisma

Prisma / NestJS : Error: Cannot find module 'src/prisma/prisma.module.js' Require stack:


I'm working on a project with Prisma and NestJS.

The error :

Error : Cannot find module 'src/prisma/prisma.module.js'
Require stack: - /home/cedric/Bureau/programmation/project_bank/project/dist/auth/auth.module.js
- /home/cedric/Bureau/programmation/project_bank/project/dist/app.module.js
- /home/cedric/Bureau/programmation/project_bank/project/dist/main.js

occured when I tried to add a @Global Decorator in my PrismaModule. I directly Ctrl+Z but the error's still there.

I think it's a compilation problem but don't know how to revert it.

Here is my code : prisma.module.ts

import { Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';

@Module({
  providers: [PrismaService],
  exports: [PrismaService],
})
export class PrismaModule {}

prisma.service.ts

import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from './prisma.service';

describe('PrismaService', () => {
  let service: PrismaService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [PrismaService],
    }).compile();

    service = module.get<PrismaService>(PrismaService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });
});

The module in which i'd like to use PrismaModule :

import { Module } from "@nestjs/common";
import { PrismaModule } from "src/prisma/prisma.module.js";
import { PrismaService } from "src/prisma/prisma.service.js";
import { AuthController } from "./auth.controller.js";
import { AuthService } from "./auth.service.js";

@Module({
    imports: [PrismaModule],
    controllers: [AuthController],
    providers: [AuthService, PrismaService],
})
export class AuthModule {}

My app.module.ts :

import { CustomersModule } from './customers/customers.module';
import { PrismaModule } from './prisma/prisma.module';

@Module({
  imports: [AuthModule, CustomersModule, PrismaModule, ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Solution

  • The solution was quite simple but I can't find it anywhere.

    In this code, the PrismaModule is imported too many times.

    In the AuthModule, we can see that the PrismaService is once imported with providers: [AuthService, PrismaService] and once imported inside the PrismaModule in imports: [PrismaModule]

    So, the correct code should be :

    auth.module.ts:

    @Module({
        imports: [PrismaModule],
        controllers: [AuthController],
        providers: [AuthService]
    })