-
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
Using ESM migration files causes ERR_REQUIRE_ESM
error
#895
Comments
Also note, I realize I can automatically create migration files with the |
Just tried a workaround by renaming all the file extensions in the migrations directory from
|
Hi @markcellus I had the same error and this is how it works on my machine :) . My package.json has type: module and I use imports across the app. As yourself I changed the file extension to .cjs. I generate the migrations with the migrate create cli command and my file will look something like: When running migrate up the ESM complain will go away. Hope it will help you as well. |
For anyone wondering how to solve this without changing any file extensions or config. The only requirement is that you need to have You can create a separate // tsconfig.migrations.json
{
// Extend the original tsconfig.json
"extends": "./tsconfig.json",
"compilerOptions": {
// Use commonjs instead of esm when compiling the migration files
"module": "CommonJS",
"moduleResolution": "Node"
},
// Tell ts-node to treat any file under the migrations directory as cjs instead of esm
// this basically overrides the "type": "module" in your package.json file
"ts-node": {
"moduleTypes": {
// Change the "migrations" key here to match the path of your migrations folder
"migrations": "cjs"
}
},
// Add the path of your migrations folder and exclude everything else
"include": ["migrations"],
"exclude": ["src"]
} And now in your "scripts": {
"migrate": "ts-node --project tsconfig.migrations.json node_modules/node-pg-migrate/bin/node-pg-migrate"
} and voila. |
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
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. {
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"moduleResolution": "bundler"
}
} Finally, |
Is this still an issue with 7.0.0-alpha.0? |
I'm new to node-pg-migrate. Coming here after encountering this problem. Disappointed, but not surprised, to see the issue is two years old. |
Which version did you test? If it is still an issue, let's tackle this and set prio to high for this one. |
I'm using node-pg-migrate version 7.3.2 My project has The error appeared when I was following the getting started instructions.
Then when I do
I 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? |
I have sooooo many issues when trying to switch to |
When a project is set to be an ES module (by setting
type
tomodule
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 thenode-pg-migrate
package itself is not an ES module?Contents of migration file are just exporting async up and down functions:
The text was updated successfully, but these errors were encountered: