prisma: NestJS: `Error: Cannot find module './generated/client'` when importing Prisma Client when using custom output path

Bug description

In a NestJS applicating, when importing prisma client using a custom generated path. note: application runs fine when importing prisma client using default path import { PrismaClient } from '@prisma/client';

application gets following error

Require stack:
- prismatest\dist\prisma\prisma.service.js
- prismatest\dist\prisma\prisma.module.js
- prismatest\dist\app.module.js
- prismatest\dist\main.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Function.Module._load (node:internal/modules/cjs/loader:920:27)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (prismatest\src\prisma\prisma.service.ts:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)`

How to reproduce

In NestJS application, set schema.prisma with custom output path

generator client {
  provider = "prisma-client-js"
  output = "./generated/client"
}

Create nestJS service and extend PrismaClient which imports from generated output PrismaClient

import { Injectable } from '@nestjs/common';
import { PrismaClient } from './generated/client';

@Injectable()
export class PrismaService extends PrismaClient {}

Expected behavior

No response

Prisma information

generator client {
  provider = "prisma-client-js"
  output = "../../node_modules/@gigauth/generated/client"
}
import { Injectable } from '@nestjs/common';
import { PrismaClient } from './generated/client';

@Injectable()
export class PrismaService extends PrismaClient {}

Environment & setup

  • OS: Windows
  • Database: PostgreSQL
  • Node.js version: v18.15.0
  • NestJS version: 10.1.8

Prisma Version

prisma                  : 5.2.0
@prisma/client          : 5.2.0
Current platform        : windows
Query Engine (Node-API) : libquery-engine 2804dc98259d2ea960602aca6b8e7fdc03c1758f (at ..\..\node_modules\@prisma\engines\query_engine-windows.dll.node)
Schema Engine           : schema-engine-cli 2804dc98259d2ea960602aca6b8e7fdc03c1758f (at ..\..\node_modules\@prisma\engines\schema-engine-windows.exe)  
Schema Wasm             : @prisma/prisma-schema-wasm 5.2.0-25.2804dc98259d2ea960602aca6b8e7fdc03c1758f
Default Engines Hash    : 2804dc98259d2ea960602aca6b8e7fdc03c1758f
Studio                  : 0.494.0

About this issue

  • Original URL
  • State: open
  • Created 10 months ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

Ugh yes, I can reproduce that removing the seed file makes the application with a custom output folder work: https://github.com/prisma/prisma-examples/pull/5234 - it now fails because it does not return any data. Not surprising with no seed 😆

That is… unexpected. Seems the seed.ts in the prisma folder leads to the app being bundled differently, without the custom output folder. :wat:

A possible workaround without deleting the prisma/ seed.ts file would be copying the files (cpx) right before you run the dev script or right after your build script (depending on your setup)

Using the rest-nestjs Prisma example, you can get around it as follows.:

  1. Install cpx as a dev dependency — npm install cpx
  2. Add a script to copy the files in your package.json file (if the output remains to be ../prisma/generated):
"copy:prisma": "cpx 'prisma/generated/**' 'dist/prisma/generated'",
  1. Update the dev script to run copy:prisma before starting the application
"dev": "npm run copy:prisma && nest start --watch",

Hm, isn’t that tree identical to what I had in https://github.com/prisma/prisma-examples/pull/5228?

(I know this is not what you want, but a custom output path that is in node_modules, e.g. ../node_modules/@foo/bar, seems to work: https://github.com/prisma/prisma-examples/pull/5229 I totally do not recommend doing that, but for some reason that is picked up.)

@janpio

Can you please do that and then link to your answer here? These questions are designed to ask for the correct details to debug such a problem.

https://github.com/prisma/prisma/discussions/20257#discussioncomment-6992382

Can you share the complete error message? This only seems to be part of it - the actual error is missing for example. Thanks!

Error: Cannot find module './generated/client'
Require stack:
- prismatest\dist\prisma\prisma.service.js
- prismatest\dist\prisma\prisma.module.js
- prismatest\dist\app.module.js
- prismatest\dist\main.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Function.Module._load (node:internal/modules/cjs/loader:920:27)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (prismatest\src\prisma\prisma.service.ts:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)