I have nest config - nestjs 10, prisma 5.6.0, swc
schema.prisma file
generator client {
provider = "prisma-client-js"
output = "../generated/client"
binaryTargets = ["native", "darwin"]
}
nest-cli file
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"builder": {
"type": "swc",
"options": {
"swcrcPath": "/.swcrc"
}
},
"typeCheck": true,
"deleteOutDir": true,
"assets": [
{
"include": "locales/**/*",
"watchAssets": true
}
],
"webpack": false
},
}
tsconfig file
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"paths": {
"generated/client": ["generated/client"],
"src/*": ["src/*"]
},
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}
I get error
Successfully compiled: 579 files with swc (534.88ms)
Watching for file changes.
Error: Cannot find module '../../generated/client/client'
Require stack:
- /usr/src/app/dist/prisma/prisma.service.js
- /usr/src/app/dist/app.module.js
- /usr/src/app/dist/main.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1053:15)
at Function.Module._load (node:internal/modules/cjs/loader:898:27)
at Module.require (node:internal/modules/cjs/loader:1120:19)
at require (node:internal/modules/helpers:112:18)
at Object.<anonymous> (/usr/src/app/dist/prisma/prisma.service.js:12:17)
at Module._compile (node:internal/modules/cjs/loader:1239:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1293:10)
at Module.load (node:internal/modules/cjs/loader:1096:32)
at Function.Module._load (node:internal/modules/cjs/loader:935:12)
at Module.require (node:internal/modules/cjs/loader:1120:19)
Also does it compile not to dist/src but simply to dist? enter image description here
I created swcrc file
{
"$schema": "https://json.schemastore.org/swcrc",
"sourceMaps": true,
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
},
"baseUrl": "./",
"paths": {
"generated/client": ["generated/client"],
"src/*": ["src/*"]
}
},
"minify": false
}
but I still get the this error
I don’t even know how to solve this problem, I created a new pet project, but there is the same error. I can’t figure out whether it’s the prisma or the SWC collector
The error message you’re encountering indicates that the compiled JavaScript code in the dist directory is looking for the prisma client module in a path that doesn’t exist or cannot be resolved correctly with the provided configuration. Here are several steps and verifications that could help resolve the problem:
1_Check Prisma Client Generation
Ensure that you have successfully run prisma generate and that the generated/client folder exists in your project’s root. The Prisma client should be generated before you attempt to run your NestJS application.
2_Adjust the Prisma Schema output Path
Your schema.prisma file indicates the output directory is ../generated/client. Verify that this is the correct relative path from the schema.prisma file’s location to the generated/client folder.
3_Verify Prisma Service Import Path
Check the import statements in prisma.service.js. They should match the actual location of the generated/client folder after building. If your outDir is set to ./dist, then the imports should be relative to that dist folder.
4_Paths Configuration
in tsconfig Your paths configuration inside tsconfig.json looks correct, but ensure that it matches the structure of your project. This configuration is mostly for development purposes, and you may need to adjust import paths for runtime if they are not resolved correctly by SWC.
5_Inspect the Build Output
Verify if the build process compiles the files to dist/src or just dist. You might need to modify your build scripts to ensure that the files are being placed in the correct directory that matches your import statements.
6_SWC Configuration
Your .swcrc seems correctly structured, but you will also want to ensure that SWC respects the paths provided in your tsconfig.json. In some cases, you might need to use a plugin or additional configuration to handle custom path aliases.
Given the information provided, you may try the following steps:
After ensuring your Prisma client is generated and placed in the expected directory, and after building your NestJS project, run it to verify if the issue persists. If it does, adjust the import path in your services that require the Prisma client to reflect its actual location in the dist directory.
If none of these solutions resolve the issue, there might be a discrepancy in how SWC handles paths compared to TypeScript. In some cases, developers have to forego path aliases in the build output, and adjust the import statements accordingly, or use a plugin or tool that can transform these paths appropriately during the build process.