当我尝试从表中获取所有记录时,我的 Next.js 项目遇到了问题。我收到的错误消息是“找不到模块:无法解析'net'”,其中导入跟踪指向我的项目中的多个文件。相关详情如下:
⨯ ./node_modules/postgres/src/connection.js:1:0
Module not found: Can't resolve 'net'
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
./node_modules/postgres/src/index.js
./src/db/index.ts
./src/db/queries.ts
./src/components/PatientsTable.tsx
./src/app/patients/page.tsx
import { patientService } from "@/db/queries";
export default async function PatientsTable() {
const patients = await patientService.list();
return <div></div>;
}
import PatientsTable from "@/components/PatientsTable";
import { Suspense } from "react";
export default function Page() {
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<PatientsTable />
</Suspense>
</div>
);
}
查询.ts
import { sql } from "drizzle-orm";
import db from ".";
import { patients, NewPatient, Patient } from "./schemas";
export const patientService = {
list: async () => {
const retrievedPatients = await db.select().from(patients);
return retrievedPatients;
},
}
package.json
{
"name": "sample_proj",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"migration:generate": "drizzle-kit generate:pg --schema=./src/db/schemas.ts",
"migration:apply": "bun run src/db/migrate.ts"
},
"dependencies": {
"@hookform/resolvers": "^3.3.2",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-toast": "^1.1.5",
"@tanstack/react-query": "^5.4.3",
"@tanstack/react-query-devtools": "^5.4.3",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"drizzle-orm": "^0.28.6",
"drizzle-zod": "^0.5.1",
"lucide-react": "^0.290.0",
"next": "13.5.7-canary.36",
"postgres": "^3.4.2",
"react": "^18",
"react-dom": "^18",
"react-hook-form": "^7.47.0",
"tailwind-merge": "^2.0.0",
"tailwindcss-animate": "^1.0.7",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10",
"dotenv": "^16.3.1",
"drizzle-kit": "^0.19.13",
"eslint": "^8",
"eslint-config-next": "14.0.0",
"pg": "^8.11.3",
"postcss": "^8",
"tailwindcss": "^3",
"typescript": "^5"
}
}
我尝试了各种故障排除步骤,包括:
使用bun run验证病人服务.list()方法作为脚本运行时是否有效,表明PostgreSQL连接在NextJs之外正常运行。我还通过 NextJs 将记录添加到数据库。
尽管做出了这些努力,但在 Next.js 中运行相同的代码时问题仍然存在。任何见解或建议将不胜感激。谢谢!
我对 drizzle 和 Nextjs 页面路由器也有同样的问题。我的解决方案是将代码重构为 API 路由。可能是因为postgres包不支持导入到客户端。希望有帮助