I've been trying to run my seed script in prisma with nextJs and TS, and receiving the following error, is there any way to fix this?

Viewed 397

in package.json, I've added the following lines:

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

and when I run this: npx prisma db seed

OUTPUT:

An error occured while running the seed command:

Error: Command failed with ENOENT: ts-node --compiler-options {"module":"CommonJS"} prisma/seed.ts
spawn ts-node ENOENT
2 Answers

This problem could be no ts-node installed. Please check your package.json file and see if there is any ts-node installed.

If not, just install ts-node with:

npm i ts-node@<specific-version>

or add -D if you are in development mode:

npm i ts-node@<specific-version> -D

In some situation, you may need to install Typescript as well:

npm i typescript@<version-compatible-with-your-tsnode>

It seems this is an issue with ts-node package. A workaround is to avoid ts-node altogether and create an NPM script to compile the file with tsc directly, execute with node, and clean up after it's been executed.

In your package.json you can add this script:

"db-seed": "tsc prisma/seed.ts && cat prisma/seed.js | node --input-type=\"commonjs\" && rm prisma/seed.js"

Your "seed" script would look like

"seed": "npm run db-seed"
Related