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

@Shinigami92
Copy link
Collaborator

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

@rialco Do you have feedback for me?

@rialco
Copy link
Author

rialco commented Jun 2, 2024

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

@rialco Do you have feedback for me?

Hey! sorry for the delay in replying back. Tested the v7.0.0-alpha.2 that you suggested in my old repo that used the proposed fix and its still not working with this new version :
Screenshot 2024-06-02 at 5 23 13 PM

@Shinigami92
Copy link
Collaborator

Shinigami92 commented Jun 2, 2024

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

@rialco Do you have feedback for me?

Hey! sorry for the delay in replying back. Tested the v7.0.0-alpha.2 that you suggested in my old repo that used the proposed fix and its still not working with this new version : Screenshot 2024-06-02 at 5 23 13 PM

Please test something out for me:
Try to execute ./node_modules/.bin/node-pg-migrate.mjs (the .mjs extension is important here

Ah, and also try the newest version, not the v7.0.0-alpha.2.
Progress was made in the last few month

@rialco
Copy link
Author

rialco commented Jun 2, 2024

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

@rialco Do you have feedback for me?

Hey! sorry for the delay in replying back. Tested the v7.0.0-alpha.2 that you suggested in my old repo that used the proposed fix and its still not working with this new version : Screenshot 2024-06-02 at 5 23 13 PM

Please test something out for me: Try to execute ./node_modules/.bin/node-pg-migrate.mjs (the .mjs extension is important here

Ah, and also try the newest version, not the v7.0.0-alpha.2. Progress was made in the last few month

I updated to the latest version and tried to execute the previous line but got this:

> ./node_modules/.bin/node-pg-migrate.mjs -m ./dist/migrations up
sh: ./node_modules/.bin/node-pg-migrate.mjs: No such file or directory

@Shinigami92
Copy link
Collaborator

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

@rialco Do you have feedback for me?

Hey! sorry for the delay in replying back. Tested the v7.0.0-alpha.2 that you suggested in my old repo that used the proposed fix and its still not working with this new version : Screenshot 2024-06-02 at 5 23 13 PM

Please test something out for me: Try to execute ./node_modules/.bin/node-pg-migrate.mjs (the .mjs extension is important here
Ah, and also try the newest version, not the v7.0.0-alpha.2. Progress was made in the last few month

I updated to the latest version and tried to execute the previous line but got this:

> ./node_modules/.bin/node-pg-migrate.mjs -m ./dist/migrations up
sh: ./node_modules/.bin/node-pg-migrate.mjs: No such file or directory

Oh this is really interesting info for me 👍

Okay, last try (https://www.unpkg.com/browse/[email protected]/bin/node-pg-migrate.mjs)
-> ./node_modules/node-pg-migrate/bin/node-pg-migrate.mjs

@rialco
Copy link
Author

rialco commented Jun 2, 2024

Collaborator

> ./node_modules/node-pg-migrate/bin/node-pg-migrate.mjs -m ./dist/migrations up

sh: ./node_modules/node-pg-migrate/bin/node-pg-migrate.mjs: Permission denied

weird getting permission denied. (just for context, I am using macos) - tried with sudo as well

@Shinigami92 Shinigami92 added c: bug Something isn't working p: 1-normal Nothing urgent and removed s: awaiting more info Additional information are requested labels Jun 2, 2024
@Shinigami92 Shinigami92 added this to the v7.x milestone Jun 2, 2024
@Shinigami92
Copy link
Collaborator

Collaborator

> ./node_modules/node-pg-migrate/bin/node-pg-migrate.mjs -m ./dist/migrations up

sh: ./node_modules/node-pg-migrate/bin/node-pg-migrate.mjs: Permission denied

weird getting permission denied. (just for context, I am using macos) - tried with sudo as well

Okay, I will try to find time for this next few days
But cannot promise anything right now, as my upcoming week is a bit planned

@Shinigami92
Copy link
Collaborator

Idea for a temp workaround
node ./node_modules/node-pg-migrate/bin/node-pg-migrate.mjs -m ./dist/migrations up

So instead if just running it and assuming sh, run it with node
Cause this file does not have a #!/usr/bin/env node as first line

@rialco
Copy link
Author

rialco commented Jun 2, 2024

Idea for a temp workaround node ./node_modules/node-pg-migrate/bin/node-pg-migrate.mjs -m ./dist/migrations up

So instead if just running it and assuming sh, run it with node Cause this file does not have a #!/usr/bin/env node as first line

Now I get this:

> node ./node_modules/node-pg-migrate/bin/node-pg-migrate.mjs -m ./dist/migrations up

ReferenceError: require is not defined in ES module scope, you can use import instead
    at tryRequire (file:///Users/ricardo/Documents/projects/sentiment-analysis/sentiment_api_service/node_modules/node-pg-migrate/bin/node-pg-migrate.mjs:20:9)
    at bin/node-pg-migrate.ts (file:///Users/ricardo/Documents/projects/sentiment-analysis/sentiment_api_service/node_modules/node-pg-migrate/bin/node-pg-migrate.mjs:195:20)
    at __require (file:///Users/ricardo/Documents/projects/sentiment-analysis/sentiment_api_service/node_modules/node-pg-migrate/bin/node-pg-migrate.mjs:4:50)
    at file:///Users/ricardo/Documents/projects/sentiment-analysis/sentiment_api_service/node_modules/node-pg-migrate/bin/node-pg-migrate.mjs:488:16
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:526:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: bug Something isn't working p: 1-normal Nothing urgent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants