-
Notifications
You must be signed in to change notification settings - Fork 176
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
Error applying migrations when using esm and latest LTS node v22.13 #458
Comments
Made very fast fix (fork) because it blocking me: can't add migration in project with existing migrations. lib/env/config.js:60 You may use |
The problem is that Node 22.12 now export default {
some: "object",
}; using Node 22.12: $ node -v
v22.12.0
$ node -p 'require("./esm.js")'
(node:13394) ExperimentalWarning: CommonJS module path/to/[eval] is loading ES Module path/to/esm.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
[Module: null prototype] {
__esModule: true,
default: { some: 'object' }
} whereas 22.11 and before throw the migrate-mongo/lib/env/config.js Lines 65 to 73 in 3753b2b
$ node -v
v22.11.0
$ node -p 'require("./esm.js")'
node:internal/modules/cjs/loader:1681
throw err;
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/jonrsharpe/workspace/classplanner/esm.js from /Users/jonrsharpe/workspace/classplanner/[eval] not supported.
Instead change the require of esm.js in /Users/jonrsharpe/workspace/classplanner/[eval] to a dynamic import() which is available in all CommonJS modules.
at TracingChannel.traceSync (node:diagnostics_channel:315:14)
at [eval]:1:1 {
code: 'ERR_REQUIRE_ESM'
} The correct fix is to add this behaviour to the non-error path, e.g.: try {
- return await Promise.resolve(moduleLoader.require(configPath));
+ const loadedRequire = await Promise.resolve(moduleLoader.require(configPath));
+ return loadedRequire.__esModule ? loadedRequire.default : loadedRequire;
} catch (e) {
if (e.code === 'ERR_REQUIRE_ESM') {
const loadedImport = await moduleLoader.import(url.pathToFileURL(configPath));
return loadedImport.default
}
throw e;
} It should also handle the fact that |
Fixed in v12 |
Unfortunately this has just moved the error; now if you try to use ESM in Node >=22.12 you get: ERROR: Could not migrate up <esm migration>.js: Expected a function TypeError: Expected a function This is because the same logic is duplicated in migrate-mongo/lib/env/migrationsDir.js Lines 97 to 108 in 6712c58
|
Weird, I cannot reproduce the issue myself with Node.js v22.13.1 + migrate-mongo using ESM. |
I've just published v12.0.1, the issue should be resolved. Can you verify? I cannot reproduce the issue. |
I was able to reproduce the issue with this sample repo: /~https://github.com/pivotal-djoo/migrate-mongo-esm-sample |
Describe the bug
Error message "ERROR: No
url
defined in config file! Error: Nourl
defined in config file!" is shown when attempting to apply migrations when using esm and node v22.13. The error does not occur using previous versions of node such as v22.10.To Reproduce
Steps to reproduce the behavior:
Sample repository demonstrating error scenario: /~https://github.com/pivotal-djoo/migrate-mongo-esm-sample
Alternatively, follow these steps:
npx migrate-mongo init -m esm
migrate-mongo-config.js
npx migrate-mongo create <migration-name>
npx migrate-mongo up
Expected behavior
Migrations should apply without any errors.
Additional context
Error message "ERROR: No
url
defined in config file! Error: Nourl
defined in config file!" is shown.The text was updated successfully, but these errors were encountered: