簡體   English   中英

如何修復 Prisma 和 typescript 中的種子文件錯誤?

[英]How can I fix an error with the seed file in Prisma and typescript?

我正在使用 Prisma、Postgres 14.5、Node 18 和 Typescript 構建后端。每次嘗試為數據庫播種時,我都會得到以下信息:

└ ➜ yarn prisma db seed
Environment variables loaded from .env
Running seed command `ts-node prisma/seed.ts` ...

An error occurred while running the seed command:
Error: Command failed with ENOENT: ts-node prisma/seed.ts
spawn ts-node ENOENT

ENOENT 錯誤表示“沒有這樣的文件或目錄”

我的package.json中有這個:

  "prisma": {
    "seed": "ts-node prisma/seed.ts"
  },

我的種子文件位於prisma/seed.ts並包含:

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  const person = await prisma.name.upsert({
    where: { id: 0 },
    update: {},
    create: {
      firstName: 'Sakiko',
    },
  });
}

main()
  .then(async () => {
    await prisma.$disconnect();
  })
  .catch(async (e) => {
    console.error(e);
    await prisma.$disconnect();
    process.exit(1);
  });

原來我缺少ts-node ,因為我們還沒有完全設置我們的構建。 所以我用yarn add ts-node修復了它。

此外,我必須簡化我們的tsconfig.json以刪除compilerOptions.rootDir ,因為 prisma 代碼位於src目錄之外。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM