vercel / pkg

Package your Node.js project into an executable
https://npmjs.com/pkg
MIT License
24.28k stars 1.01k forks source link

Dynamic imports issue with 'express-openapi' #1983

Closed albertlaiuste closed 9 months ago

albertlaiuste commented 1 year ago

What version of pkg are you using?

5.8.1

What version of Node.js are you using?

16.18.1

What operating system are you using?

Debian 12

What CPU architecture are you using?

x86_64

What Node versions, OSs and CPU architectures are you building for?

node16-linux-x64

Describe the Bug

I encountered an issue with express-openapi when using it in conjunction with the pkg, like: pkg . --no-bytecode --public-packages "*" --public. The issue arises when express-openapi tries to dynamically import modules using import with the file:// protocol in a bundled environment, like: const imported = await import(`file://${fsRoutesItem.path}`). It seems that the library is attempting to load modules from the local filesystem rather than from within the bundled package.

I understand that this may be a known issue, but I'm sharing it here in case anyone else encounters a similar problem with the library. I also want to provide a temporary solution that worked for me.

Solution: I was able to resolve this issue by manually patching the await import line in my package.json configuration, like so:

 "pkg": {
    "scripts": [
      "build/**/*.js"
    ],
    "patches": {
      "./node_modules/openapi-framework/dist/index.js": [
        "const imported = await import(`file://${fsRoutesItem.path}`);",
        "const imported = require(fsRoutesItem.path);"
      ]
    },
    "targets": [
      "node16-linux-x64"
    ]
  }

Expected Behavior

In a bundled environment, express-openapi should correctly load modules from within the bundled package, rather than attempting to load them from the local filesystem.

To Reproduce

  1. Create a Node.js application that uses express-openapi and pass the paths to it, like:
    initialize({
    app,
    paths: path.join(__dirname, '../build/api/routes'),
    });
  2. Bundle the application using the pkg utility.
  3. Run the bundled application.
  4. Observe that express-openapi attempts to load modules from the local filesystem instead of the bundled package.
github-actions[bot] commented 9 months ago

This issue is stale because it has been open 90 days with no activity. Remove the stale label or comment or this will be closed in 5 days. To ignore this issue entirely you can add the no-stale label

github-actions[bot] commented 9 months ago

This issue is now closed due to inactivity, you can of course reopen or reference this issue if you see fit.