-
-
Notifications
You must be signed in to change notification settings - Fork 158
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 (Kinda) with ES Modules ESM - Solution Enclosed #475
Comments
Thanks for the report! I agree, given esmodules are the way of the future they should certainly be supported. My team is using umzug v3 in production with commonjs so I'm also not ready to stop supporting that. However it may be possible to allow both by adding an optional Note: it's unlikely anything will be back ported to 2.x - but after this I think it will be safe to release v3 stable (finally). Edit: I realised after writing this that this is already supported in v3.x. You can just use a custom |
Dynamic imports are async, they need to be awaited, which I believe is not done currently for the |
@B4nan that's right, the import needs to be moved down into the |
Oh I totally missed that, thanks! Would be still cleaner to have the |
fwiw switching to |
FWIW: I am stuck currently with Umzug 2.x in a Next.js app, and tried using a (Basically: forget umzug!)... const seederPath = 'db/seeders'
const seedFiles = fs.readdirSync(seederPath);
const seederImports = seedFiles.map(async fileName => {
return import(`${seederPath}/${fileName}`)
})
async function runSeeders() {
for (const promise of seederImports) {
const seeder = await promise
await seeder.up(sequelize.getQueryInterface(), Sequelize)
}
} Warning: This will not record which migrations have been run anywhere, so if that is a requirement, you'll have to handle that separately. |
BUT, I am very much looking forward to the day umzug uses import instead of require, and everything just works (if @joeldenning's insight is correct) 😄 |
When you say stuck on v2, what do you mean exactly? What's preventing updating to v3? There are examples for |
Oh thanks for the follow-up @mmkal! Just stuck currently due to using sequelize-cli, which is (AFAIK) not compatible with 3.x yet. Also, I didn't mean to sound as if I'm against using umzug in general, we're definitely still using umzug for our migrations! |
Hi, thanks for the module. Very useful and appreciated.
Opening this for discussion and to help others.
I have the pleasure/misfortune of dealing with ES Modules (using
import
) instead of CommonJS (require()
).Everything works fine normally, and Line 75 in
lib/migration.js
is giving issues.75: return require(this.path);
To solve, I did some research and experimentation to find a simple solution.
Replacing:
return require(this.path);
with
return import(this.path);
solves it for ES Modules.
This can be accomplished with
sed
to swap out the line.sed -i'.bak' 's/require(this.path);/import(this.path);/g' ./node_modules/umzug/lib/migration.js
Why it fails:
require()
-ing the migration file(s) does not make ES Modules happyrequire()
replacing withimport()
allows the migration file(s) to load as expectedI'm not sure how to support both CommonJS and ES Modules.
The issue likely stems from umzug being a module with its own
package.json
and namespace, then mixing in the migration files from our project which use the project's ES Modulepackage.json
and namespace.More on ES Modules at this link if needed
Stats:
package.json
by adding"type": "module",
)Any suggestions?
Thank you!
The text was updated successfully, but these errors were encountered: