db-migrate / node-db-migrate

Database migration framework for node
Other
2.32k stars 360 forks source link

Still having problems with db-migrate and ESM node projects... #794

Closed kelmore5 closed 2 years ago

kelmore5 commented 2 years ago

I'm submitting a...

Current behavior

I'm still having trouble running db-migrate in an ESM project / module (on the latest beta 1.0.0-beta.18). I keep receiving this error:

[ERROR] Error [ERR_REQUIRE_ESM]: require() of ES Module /home/kyle/web/server/migrations/some-migration.js from /home/kyle/web/server/node_modules/db-migrate/lib/file.js not supported. Instead change the require of some-migration.js in /home/kyle/web/server/node_modules/db-migrate/lib/file.js to a dynamic import() which is available in all CommonJS modules.

Minimal reproduction of the problem with instructions

My migration file looks something like this:

const Table = require('../dist/database/tables/SomeTable.js').SomeTable;
const table = new Table();

...

exports.up = function (db) {
  return table.up(db);
};

...

I've also tried swapping the first line for something like this:

import {SomeTable} from '../dist/database/tables/SomeTable.js';
...

but neither have worked. Does it have something to do with me importing a file into the migration file? The imported table is from an ESM module that also has dependencies to other ESM (local) projects.

I've recently switched my projects to ESM, so let me know if I'm missing something.

Environment


db-migrate version: 1.0.0-beta.18
plugins with versions: none
db-migrate driver with versions: db-migrate-mysql 2.2.0

Additional information:
- Node version: 16.16.0
- Platform:  Ubuntu Server 18.04.6

Related Links

#721 & #784 Supposedly solved in #724

Edit: Code blocks

wzrdtales commented 2 years ago

724 is a proven fix for this. However, this fix expects you to use the version with that fix in the first place. The routine added here runs only when you run the db-migrate create command once. It adds a package.json in your migrations folder and after that works. Just create a dummy migration with the command and delete it and check for the package.json created in the migrations folder, after that it should work fine.

kelmore5 commented 2 years ago

Thanks for the quick response. However, I'm still getting the same error even with the package.json file in the migrations folder

Now, if my migration file looks like this:

const Table = require('../dist/database/tables/SomeTable.js').SomeTable;
const table = new Table();

...

exports.up = function (db) {
  return table.up(db);
};

...

I get the same error as above (Error [ERR_REQUIRE_ESM])

But if my migration file looks like this:

import {SomeTable} from '../dist/database/tables/SomeTable.js';
...

I get this error:

[ERROR] unhandledRejection
[ERROR] /home/kyle/web/EbayServer/migrations/some-migration.js:3
import {SomeTable} from '../dist/database/tables/SomeTable.js';
^^^^^^

SyntaxError: Cannot use import statement outside a module
...

Is there something else I need to do? Thanks!

Edit: Code block

wzrdtales commented 2 years ago

Well please read the models docs about esmodules for that.

Sent from Nine

--

Best

-- Tobias Gurtzick CEO WizardTales GmbH

Additional Contacts: @github | @.***

Website: https://www.wizardtales.com @.*** nmlpm: -- coming soon & open source --

Please tag your mails with "request", "issue" or "proposal", unless none of these fit or there already exists a thread.

-- Why should one reach for what one may be capable reaching, that's awfully boring and after all one still be ones yesterday being. --

Sitz der Gesellschaft: Ratingen Kokkolastr. 5 40882 Ratingen Germany

Registergericht: Amtsgericht Düsseldorf, HRB 84140 Geschäftsführer: Tobias Gurtzick


Von: Kyle Elmore @.***> Gesendet: Samstag, 23. Juli 2022 17:57 An: db-migrate/node-db-migrate Cc: Tobias Gurtzick; State change Betreff: Re: [db-migrate/node-db-migrate] Still having problems with db-migrate and ESM node projects... (Issue #794)

Thanks for the quick response. However, I'm still getting the same error even with the package.json file in the migrations folder Now, if my migration file looks like this:

const Table = require('../dist/database/tables/SomeTable.js').SomeTable; const table = new Table();

...

exports.up = function (db) { return table.up(db); };

...

I get the same error as above (Error [ERR_REQUIRE_ESM]) But if my migration file looks like this:

import {SomeTable} from '../dist/database/tables/SomeTable.js'; ...

I get this error:

[ERROR] unhandledRejection [ERROR] /home/kyle/web/EbayServer/migrations/some-migration.js:3 import {SomeTable} from '../dist/database/tables/SomeTable.js'; ^^^^^^

SyntaxError: Cannot use import statement outside a module

Is there something else I need to do? Thanks! — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you modified the open/close state.Message ID: @.***>

kelmore5 commented 2 years ago

I don't mean to be a bother, but what models doc? I don't think the db-migrate docs mentions ESM...

Appreciate your time btw, Tobias!

wzrdtales commented 2 years ago

b/c this has nothing to do with us. We support for db-migrate, not for understanding nodejs.

as a little favor:

https://nodejs.org/api/esm.html#interoperability-with-commonjs https://pencilflip.medium.com/using-es-modules-with-commonjs-modules-in-node-js-1015786dab03

kelmore5 commented 2 years ago

Ah!

Thanks Tobias

kelmore5 commented 2 years ago

Okay, I'm pretty sure now that what I'm trying to do won't work. I.e. I don't believe you can import a file from an ES module into the migration file

Meaning, anything I import into the migration file has to come from a CommonJS module, and that if I want to do above^, I'd have to convert at least those imports into CJS files.

Makes sense! Just leaving a comment in case anyone else needs it. Thanks again for the help!