mxcdh
mxcdh

Reputation: 1113

prisma db seed & typescript problem with import and type:module

I learning prisma from this tutorial: https://www.prisma.io/blog/fullstack-nextjs-graphql-prisma-oklidw1rhw

I have problem with step: npx prisma db seed --preview-feature

I copy 1;1 code from this tutorial, and when I run: npx prisma db seed --preview-feature

I get error:

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

When I add'ed to package.json "type":"module", I get error:


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

I reading similar problem on StackOverflow, github etc, but any case not working, anyone can help me?

Upvotes: 6

Views: 13894

Answers (3)

VanDjango
VanDjango

Reputation: 31

I've had the same problem, while using t3 stack.

Install tsx

npm install tsx

In package.json

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

In 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);
  });

Then run

npm run db-seed

This was solved thanks to the T3 prisma documentation

Upvotes: 3

Allen
Allen

Reputation: 4759

Another "painless" option using tsx, npm i -D tsx, then

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

Detail if interested.

Upvotes: 21

Nelloverflow
Nelloverflow

Reputation: 1756

Try and double-check your seed command in the prisma section of your package.json, it should look like the one in the repo of the guide you've linked:

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

From the error you've pasted you're missing the --compiler-options parameter.

Upvotes: 14

Related Questions