Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Bug Report: ERR_MODULE_NOT_FOUND When Running Database Seed Script #501

Open
2 tasks done
x15sr71 opened this issue Oct 12, 2024 · 6 comments
Open
2 tasks done

Comments

@x15sr71
Copy link

x15sr71 commented Oct 12, 2024

📜 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?

  • I checked and didn't find a similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to submit PR?

Yes I am willing to submit a PR!

@x15sr71
Copy link
Author

x15sr71 commented Oct 12, 2024

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

@CuriousCoder00
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Author

x15sr71 commented Oct 12, 2024

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
Copy link

so is it open or solved?

@x15sr71
Copy link
Author

x15sr71 commented Oct 14, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants