开发者问题收集

prisma db seed 和 typescript 存在 import 和 type:module 问题

2022-04-09
11918

我从本教程中学习 prisma: https://www.prisma.io/blog/fullstack-nextjs-graphql-prisma-oklidw1rhw

我在步骤中遇到问题: npx prisma db seed --preview-feature

我从本教程中复制了 1;1 代码,当我运行时: npx prisma db seed --preview-feature

我收到错误:

prisma:warn Prisma "db seed" was in Preview and is now Generally Available.
You can now remove the --preview-feature flag.
prisma:warn The "ts-node" script in the package.json is not used anymore since version 3.0 and can now be removed.
Environment variables loaded from .env
Running seed command `ts-node prisma/seed.ts` ...
(node:34420) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/m/dev/prisma/awesome-links/part-1/prisma/seed.ts:38
import Prisma from '@prisma/client';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._compile (node:internal/modules/cjs/loader:1067:27)
    at Module.m._compile (/Users/m/dev/prisma/awesome-links/part-1/node_modules/ts-node/src/index.ts:1455:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/m/dev/prisma/awesome-links/part-1/node_modules/ts-node/src/index.ts:1458:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at phase4 (/Users/m/dev/prisma/awesome-links/part-1/node_modules/ts-node/src/bin.ts:567:12)

An error occured while running the seed command:
Error: Command failed with exit code 1: ts-node prisma/seed.ts

当我将“type”添加到 package.json 时:“module”,我收到错误:


m@ms-MacBook-Pro ~/d/p/a/part-1 (part-1) [1]> npx prisma db seed --preview-feature
prisma:warn Prisma "db seed" was in Preview and is now Generally Available.
You can now remove the --preview-feature flag.
prisma:warn The "ts-node" script in the package.json is not used anymore since version 3.0 and can now be removed.
Environment variables loaded from .env
Running seed command `ts-node prisma/seed.ts` ...
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/m/dev/prisma/awesome-links/part-1/prisma/seed.ts
    at new NodeError (node:internal/errors:371:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:87:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:102:38)
    at defaultLoad (node:internal/modules/esm/load:21:14)
    at ESMLoader.load (node:internal/modules/esm/loader:359:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:280:58)
    at new ModuleJob (node:internal/modules/esm/module_job:66:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:297:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:261:34)
    at async Promise.all (index 0) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

An error occured while running the seed command:
Error: Command failed with exit code 1: ts-node prisma/seed.ts

我读到过类似的问题在 StackOverflow、github 等上,但任何情况下都不起作用,有人可以帮助我吗?

3个回答

另一个“无痛”选项是使用 tsx npm i -D tsx ,然后

"prisma": {
    "seed": "tsx prisma/seed.ts"
},

Detail (如果感兴趣)。

Allen
2023-03-06

尝试并仔细检查 package.json 的 prisma 部分中的种子命令,它应该看起来像 您链接的指南的存储库 中的命令:

 "prisma": {
    "seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
  },

从您粘贴的错误来看,您缺少 --compiler-options 参数。

Nelloverflow
2022-04-09

我在使用 t3 堆栈时遇到了同样的问题。

安装 tsx

npm install tsx

在 package.json 中

{
  "scripts": {
    "db-seed": "NODE_ENV=development prisma db seed"
  },
  "prisma": {
    "seed": "tsx prisma/seed.ts"
  }
}

在 prisma/seed.ts 中

import { prisma } from "../src/server/db";

async function main() {
  const id = "cl9ebqhxk00003b600tymydho";
  await prisma.example.upsert({
    where: {
      id,
    },
    create: {
      id,
    },
    update: {},
  });
}

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

然后运行

npm run db-seed

感谢 T3 prisma 文档

解决了这个问题
VanDjango
2023-04-29