1

I am following the NestJs Prisma docs. I followed every step but I keep getting this error: Cannot find module 'generated/prisma' or its corresponding type declarations.ts(2307) in this line: import { User as UserModel, Post as PostModel } from 'generated/prisma'; even though this is what the docs are using. The suggested import is from import { User as UserModel, Post as PostModel } from 'generated/prisma/client'; but when running the app I get another error:

Object.defineProperty(exports, "__esModule", { value: true });
                      ^

ReferenceError: exports is not defined
    at file:///D:/code/nestjs/nest-prisma/dist/generated/prisma/client.js:38:23

The only time it works is when I follow this prisma example. But this one does not create a prisma.config.ts nor does it provide a generator output like the nestjs docs shows, which as far as i understand will be mandatory from Prisma ORM version 7

1 Answer 1

1

In my case the problem was in folder 'distr', which dont has folder "generated/prisma" after command npm run build.

Solution:

  1. Delete outputline from file "prisma/schema.prisma"

    output          = "../generated/prisma"
    
    // This is your Prisma schema file,
    // learn more about it in the docs: https://pris.ly/d/prisma-schema
    
    // Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
    // Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
    
    generator client {
      provider = "prisma-client-js"  
    }
    
    datasource db {
      provider = "postgresql"
      url      = env("DATABASE_URL")
    }
    
    model Role {
      id            Int      @default(autoincrement()) @id
      name          String   @unique
      alias         String   @unique
      description   String?
      blocked       Boolean  @default(false)
      createdAt     DateTime @default(now())
      updatedAt     DateTime @updatedAt
    }
    

    Now prisma will genereate output to /node_modules/@prisma/client

  2. Run command: npx prisma generate

  3. Reopen IDE.

  4. In src/prisma.service.ts add import PrismaClient

    import { Injectable, OnModuleInit } from '@nestjs/common';
    import { PrismaClient } from '@prisma/client';
    
    @Injectable()
    export class PrismaService extends PrismaClient implements OnModuleInit {
      async onModuleInit() {
        await this.$connect();
      }
    
      async onModuleDestroy() {
        await this.$disconnect();
      }
    }
    
  5. Add or edit file src/prisma.module.ts:

    import { Global, Module } from '@nestjs/common';
    import { PrismaService } from './prisma.service';
    
    @Global() // set module as global
    @Module({
      providers: [PrismaService],
      exports: [PrismaService],
    })
    export class PrismaModule {}
    
  6. Add src/prisma.module.ts to src/app.module.ts:

    import { Module } from '@nestjs/common';
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
    import { RolesModule } from './roles/roles.module';
    import { PrismaModule } from './prisma.module';
    
    @Module({
      imports: [RolesModule, PrismaModule],
      controllers: [AppController],
      providers: [AppService],
    })
    export class AppModule {}
    
    
  7. In service (for example: src/roles/roles.service.ts) add import to role-model:

    import { Injectable } from '@nestjs/common';
    import { CreateRoleDto } from './dto/create-role.dto';
    import { UpdateRoleDto } from './dto/update-role.dto';
    import { PrismaService } from '../prisma.service';
    import { Role } from '@prisma/client';
    
    @Injectable()
    export class RolesService {
      constructor(private prisma: PrismaService) {}
    
      async create(createRoleDto: CreateRoleDto): Promise<Role> {
        console.log(createRoleDto);
        const role = await this.prisma.role.create({ data: createRoleDto });
        return role;
      }
    
      async findAll(): Promise<Role[]> {
        const roles = await this.prisma.role.findMany();
        return roles;
      }
    
      async findOne(id: string): Promise<Role | null> {
        const role = await this.prisma.role.findUnique({
          where: { id: Number(id) },
        });
        return role;
      }
    
      async update(id: string, data: UpdateRoleDto): Promise<Role> {
        const role = await this.prisma.role.update({
          data,
          where: { id: Number(id) },
        });
        return role;
      }
    
      async remove(id: string): Promise<Role> {
        const role = await this.prisma.role.delete({
          where: { id: Number(id) },
        });
        return role;
      }
    }
    
    
  8. Run command: npm run build

  9. Run command: npm start

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.