我有 Nest 配置 - NestJS 10、Prisma 5.6.0、SWC
schema.prisma 文件
generator client {
provider = "prisma-client-js"
output = "../generated/client"
binaryTargets = ["native", "darwin"]
}
nest-cli 文件
{
"$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 文件
{
"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
}
}
我收到错误
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)
它是否也不会编译为 dist/src 而只是编译为 dist ?
我创建了 swcrc 文件
{
"$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
}
但我仍然收到此错误
我什至不知道如何解决这个问题,我创建了一个新的pet项目,但还是有同样的错误。我分不清是prisma还是SWC收集器了
您遇到的错误消息表明 dist 目录中编译的 JavaScript 代码正在寻找不存在或无法使用提供的配置正确解析的路径中的 prisma 客户端模块。以下是有助于解决问题的几个步骤和验证:
1_检查 Prisma 客户端生成
确保您已成功运行 prismagenerate 并且 generated/client 文件夹存在于项目的根目录中。在尝试运行 NestJS 应用程序之前,应生成 Prisma 客户端。
2_调整Prisma Schema输出路径
您的 schema.prisma 文件指示输出目录是 ../ generated/client。验证这是否是从 schema.prisma 文件位置到 generated/client 文件夹的正确相对路径。
3_验证Prisma服务导入路径
检查 prisma.service.js 中的导入语句。它们应该与构建后生成的/客户端文件夹的实际位置相匹配。如果您的 outDir 设置为 ./dist,则导入应该相对于该 dist 文件夹。
4_路径配置
在 tsconfig 中 tsconfig.json 中的路径配置看起来正确,但请确保它与项目的结构匹配。此配置主要用于开发目的,如果 SWC 未正确解析它们,您可能需要调整运行时的导入路径。
5_检查构建输出
验证构建过程是否将文件编译到 dist/src 或仅编译到 dist。您可能需要修改构建脚本以确保将文件放置在与导入语句匹配的正确目录中。
6_SWC配置
您的 .swcrc 似乎结构正确,但您还需要确保 SWC 尊重 tsconfig.json 中提供的路径。在某些情况下,您可能需要使用插件或其他配置来处理自定义路径别名。
根据所提供的信息,您可以尝试以下步骤:
npx prisma generate
nest build
确保生成 Prisma 客户端并将其放置在预期目录中,并构建 NestJS 项目后,运行它以验证问题是否仍然存在。如果是这样,请调整服务中的导入路径,要求 Prisma 客户端反映其在 dist 目录中的实际位置。
如果这些解决方案都不能解决问题,则 SWC 处理路径的方式与 TypeScript 相比可能存在差异。在某些情况下,开发人员必须放弃构建输出中的路径别名,并相应地调整导入语句,或者使用可以在构建过程中适当转换这些路径的插件或工具。