Skip to content
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

fix: change from require to dynamic import #893

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rialco
Copy link

@rialco rialco commented May 15, 2022

Summary

Change the way the migrations files are being loaded in the loadMigrations function. Replace from require to a dynamic import await import in order to support ESM projects.

Known issues

In a pure ESM project the node-pg-migrate module is not usable because the loadMigrations function uses require instead of await import.

Current project characteristics:

  • Typescript
  • package.json : "type" : "module"

tsconfig.json content:

{
  "extends": "@tsconfig/node16/tsconfig.json",
  "compilerOptions": {
    "lib": ["es2021"],
    "module": "es2022",
    "target": "es2022",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": true,
    "baseUrl": "./src",
    "outDir": "dist",
  },
  "ts-node": {
    "esm": true
  },
  "include": ["./src", "./migrations"]
}

Script used to run the migrations:

"migrate:build" : "./node_modules/.bin/node-pg-migrate -m ./dist/migrations"

Logs

Error: Can't get migration files: Error [ERR_REQUIRE_ESM]: require() of ES Module .../dist/migrations/1652421258856_add-tweets-table.js from .../node_modules/node-pg-migrate/dist/runner.js not supported.
Instead change the require of 1652421258856_add-tweets-table.js in ..../node_modules/node-pg-migrate/dist/runner.js to a dynamic import() which is available in all CommonJS modules.

@lamh85
Copy link

lamh85 commented Nov 14, 2022

Can someone please review this? I too experience this bug.

@Shinigami92
Copy link
Collaborator

To all participants subscribed to this PR: please try out v7.0.0-alpha.2 and report if it is still broken.

@Shinigami92 Shinigami92 added the s: awaiting more info Additional information are requested label Apr 5, 2024
@Shinigami92 Shinigami92 self-requested a review April 5, 2024 22:40
@rialco
Copy link
Author

rialco commented Apr 10, 2024

Will test this in the following days, and provide feedback. thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s: awaiting more info Additional information are requested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants