code100x / job-board

100xdevs job board - temp domain attached ( will update soon )
https://job.vineet.tech
313 stars 448 forks source link

πŸ› Bug Report: ERR_MODULE_NOT_FOUND When Running Database Seed Script #501

Closed x15sr71 closed 2 weeks ago

x15sr71 commented 3 weeks ago

πŸ“œ Description

When attempting to run the database seed script using the command: npm run db:seed

πŸ‘Ÿ Reproduction steps

  1. Clone the repository.
  2. Navigate to the project directory.
  3. Ensure that the environment variables are set in the .env file.
  4. Run the command npm run db:seed

πŸ‘ Expected behavior

The database seed script should run without any errors, and the data should be seeded successfully.

πŸ‘Ž Actual Behavior with Screenshots

An ERR_MODULE_NOT_FOUND error is thrown, indicating that the package cannot be found. Screenshot 2024-10-12 150332

OS name

Windows

browser name

Firefox

npm version

No response

node version

No response

πŸ“ƒ Provide any additional context for the Bug.

The issue seems to stem from the way the --import flag is used in the command. The command that causes the error: node --import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));' prisma/seed.ts

possible fix would be to use the --loader option instead of --import.

πŸ‘€ Have you spent some time to check if this bug has been raised before?

🏒 Have you read the Contributing Guidelines?

Are you willing to submit PR?

Yes I am willing to submit a PR!

x15sr71 commented 3 weeks ago

@VineeTagarwaL-code I would like to raise a PR to fix this issue; please assign it to me

CuriousCoder00 commented 3 weeks ago

There's not a kind of fix issue for this...

Before making the change seed script was this:

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

and it was also raising this issue

After making the change:

"prisma": { "seed": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' prisma/seed.ts" },

CuriousCoder00 commented 3 weeks ago

both seeding scripts were failing on my side so i had to think of alternate solution that was to use tsx instead of ts-node

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

Not sure if it works on your end

x15sr71 commented 3 weeks ago

Thanks for the insights, @CuriousCoder00!

I found that the issue arises from the use of the --import flag in the seeding script. After some investigation, the ERR_MODULE_NOT_FOUND occurs because the --import flag isn’t properly supported in this context.

To fix this, I will switch to using the --loader flag in the scripts section of the package.json as it provides a more stable and expected behavior with ES module loading.

I have tested it, and it works on my end.

Final script would look something like following in the scripts in package.json:

"scripts": { "db:seed": "npx prisma db push && node --loader ts-node/esm prisma/seed.ts" }

NalinDalal commented 3 weeks ago

so is it open or solved?

x15sr71 commented 3 weeks ago

@NalinDalal i raised pr which solves the issue, but isn't merged yet

VineeTagarwaL-code commented 2 weeks ago

nice work, make sure you post on x about your pr merge happy coding :rocket: