DEV Community

Mekzy
Mekzy

Posted on

Resolving 'Must use import to load ES Module' error while using Sequelize ORM and ES6 syntax in Node.js"

Recently, I attempted to set up a Node.js project using the Sequelize ORM and ES6 syntax. While creating migrations, I encountered an error stating "Must use import to load ES Module: /Users/[user_path]/project_name/src/migrations/[migration_name] require() of ES modules is not supported."

The error suggested three solutions:

  • Rename the migration file to end with .cjs
  • Use the import() function instead of require()
  • Remove "type": "module" from the root directory's package.json

However, I was reluctant to use any of these solutions as they would have required extensive changes to my codebase. Instead, I turned to the web for more information and found a simple solution.

The fix involved creating a new package.json file in the migrations folder with the following code:

{
  "type": "commonjs"
}
Enter fullscreen mode Exit fullscreen mode

This change allowed my code to run successfully. If you have any other suggestions for resolving this issue, please let me know.

Top comments (3)

Collapse
 
iamrdwn profile image
Ridwan

I just came to say that you stopped me from "doing some extensive changes to my codebase". Thank you :D

Collapse
 
emekaofe profile image
Mekzy

Glad to hear that

Collapse
 
saulodias profile image
Saulo Dias

Thanks man! I actually did not need to use ES modules for my project and that works fine and gives me prettier imports as well.