node-pg-migrate: Using ESM migration files causes `ERR_REQUIRE_ESM` error
When a project is set to be an ES module (by setting type to module in package.json), adding a migration file that ends in .js extension in the migrations directory returns the following error when running migration. I suspect this is because the node-pg-migrate package itself is not an ES module?
/code/node_modules/node-pg-migrate/dist/runner.js:35
throw new Error(`Can't get migration files: ${err.stack}`);
^
Error: Can't get migration files: Error [ERR_REQUIRE_ESM]: require() of ES Module /code/db/migrations/0000-instance-schema.js from /code/node_modules/node-pg-migrate/dist/runner.js not supported.
Instead change the require of 0000-instance-schema.js in /code/node_modules/node-pg-migrate/dist/runner.js to a dynamic import() which is available in all CommonJS modules.
at /code/node_modules/node-pg-migrate/dist/runner.js:24:21
at Array.map (<anonymous>)
at loadMigrations (/code/node_modules/node-pg-migrate/dist/runner.js:19:41)
at async Promise.all (index 0)
at async Object.exports.default (/code/node_modules/node-pg-migrate/dist/runner.js:151:40)
at loadMigrations (/code/node_modules/node-pg-migrate/dist/runner.js:35:15)
at async Promise.all (index 0)
at async Object.exports.default (/code/node_modules/node-pg-migrate/dist/runner.js:151:40)
Contents of migration file are just exporting async up and down functions:
export async function up() {
// do stuff here
}
export async function down() {
// do stuff here
}
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 11
- Comments: 20
For anyone wondering how to solve this without changing any file extensions or config. The only requirement is that you need to have
ts-nodeinstalled.You can create a separate
tsconfig.jsonfile that’ll only be used for migrations with the following contents, you can call the file any name you want but here I’ll be usingtsconfig.migrations.jsonAnd now in your
package.jsonfile change yourmigratescript to the followingand voila.
To all that are subscribed to this issue: please checkout the https://github.com/salsita/node-pg-migrate/releases/tag/v8.0.0-rc.0 and tell if this is fixed. Thanks 🙏
The new release works perfectly. Now, the create command generates migration files with ESM export by default. Thanks 🙏
Everything I do (open source) is public and immediately published:
so that is the defacto state
For anyone else going down this rabbit hole, I was able to get past this and use typescript files with the following recipe. What it basically does is transpiles the ts file into commonjs format, which is compatible with
node-pg-migrate’s runner.db(this may not be strictly necessary but keeps things organized.db: package.json: tsconfig.migrations.json:node-pg-migrateto use these files, using command line args, environment variables, orconfig. I did the latter: config/default.json:That should do it. For reference, I’m using v6.2.2 and TS 5.
My main tsconfig file is this. I don’t know if anything here is required, but I’m including it just in case.
Finally,
template.tslisted above isn’t strictly necessary, but addsimport typeto the import statement which quiets an error down in my editor.Thanks , this is working fine with with v8.0.0-rc.0
@Shinigami92 that’s not what I meant. I won’t contribute on something you are already working on if I don’t know what you are already doing and what I can do to help you to achieve this specific task
Absolut, I cant promise anything right now, but maybe I can find some time in this month and then already remove support for node 18 and cjs early.
I’m using node-pg-migrate version 7.3.2 node version 20.12.2 npm version 10.5.0 pg version 8.11.5 MacOS Sonoma 14.4.1 if that matters
My project has
type: "module"in the package.jsonThe error appeared when I was following the getting started instructions.
npm run migrate create my-first-migrationThen when I do
npm run migrate upthe error appearsI solved it by renaming the file to .cjs as per the instructions, and now it works just fine. Even so, I don’t think it’s great that this error should ever appear in the first place. Perhaps its as simple as naming the file .cjs by default?