next.js prisma db seed & typescript导入和类型出现问题:模块

xpszyzbs  于 2023-03-08  发布在  TypeScript
关注(0)|答案(2)|浏览(214)

我学习棱镜从这个教程: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

当我添加'ed到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等网站上看到过类似的问题,但是任何情况下都不起作用,有人能帮助我吗?

f87krz0w

f87krz0w1#

尝试并仔细检查package.json的prisma部分中的seed命令,它应该与the repo of the guide you've linked中的命令类似:

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

在粘贴的错误中,缺少--compiler-options参数。

yeotifhr

yeotifhr2#

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

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

Detail如果感兴趣。

相关问题