导入和类型:模块的 prisma db 种子和打字稿问题

2023-11-24

我从本教程中学习了 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

当我添加到 package.json "type":"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 等上读到类似的问题,但无论如何都不起作用,有人可以帮助我吗?


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

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

Detail如果有兴趣。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

导入和类型:模块的 prisma db 种子和打字稿问题 的相关文章

随机推荐