question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

I’m submitting a…

  • Bug report
  • Feature request
  • Question

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
wzrdtalescommented, Jul 24, 2022

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

1reaction
wzrdtalescommented, Jul 24, 2022

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: @.***>

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable of get running a pure ESM node project - Stack Overflow
It does appear that you have another problem. You need to tell, not just TypeScript, but Node, how you plan to resolve modules....
Read more >
Migrations | Knex.js
Migrations allow for you to define sets of schema changes so upgrading a database is a breeze. Migration CLI #. The migration CLI...
Read more >
Using Node.js ES modules and top-level await in AWS Lambda
I have created a project that contains two Lambda functions, one that contains an ES module and one that contains a CommonJS module....
Read more >
Using CLI - typeorm - GitBook
This CLI tool is written in javascript and to be run on node. If your entity files are in typescript, you will need...
Read more >
TypeScript and native ESM on Node.js - 2ality
The GitHub repository iterable is an example of a TypeScript ESM package that works on Node.js. It still uses the "typesVersions" workaround ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found