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

Error: Dynamic require of "fs" is not supported #885

Closed
ad-si opened this issue May 15, 2022 · 8 comments
Closed

Error: Dynamic require of "fs" is not supported #885

ad-si opened this issue May 15, 2022 · 8 comments

Comments

@ad-si
Copy link

ad-si commented May 15, 2022

When I build https://github.com/feramhq/transity with following command:

spago bundle-app \
  --platform=node \
  --minify

And try to execute it like node index.js, I get following error:

Error: Dynamic require of "fs" is not supported
    at file:///Users/adrian/Dropbox/Projects/transity/index.js:1:418
    at file:///Users/adrian/Dropbox/Projects/transity/index.js:10:59752
    at file:///Users/adrian/Dropbox/Projects/transity/index.js:1:498
    at file:///Users/adrian/Dropbox/Projects/transity/index.js:13:53377
    at file:///Users/adrian/Dropbox/Projects/transity/index.js:1:498
    at file:///Users/adrian/Dropbox/Projects/transity/index.js:86:1476
    at ModuleJob.run (node:internal/modules/esm/module_job:175:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)

How can this be fixed? Do I need to configure esbuild somehow?

@f-f
Copy link
Member

f-f commented May 15, 2022

@sigma-andex any idea about this?

@sigma-andex
Copy link
Contributor

@ad-si can you push the branch where you have the changes?

@ad-si
Copy link
Author

ad-si commented May 15, 2022

@sigma-andex Should already have been pusht to master! 🤔

@sigma-andex
Copy link
Contributor

@ad-si I finally found some time to look into this. I think the issue is that your archiver dependency still is on CommonJS. And apparently esbuild doesn't bundle this properly. I have tried not bundling by creating an index.js

import { main } from "./output/Main/index.js"
main()

and when you run it it works fine. This is because node supports a mixed mode with ES Modules and CommonJS.

I think this is not really an issue with spago but more with esbuild or archiver not being on ESM. I think the proper way would be to update archiver to ES modules. Alternatively you can try to bundle with webpack or parcel. I think they did a better job at bundling with outdated CJS.

@CarstenKoenig
Copy link

Hi I already commented in the discourse-thread and I hope you don't mind me repeating it here.

For me it seems as if spago is asking esbuild to produce ESM:

[debug] Running command: `esbuild --platform=node --format=esm --bundle --outfile=test.js`

when I do a bundle and I guess it gets confused here - if I don't include the --format=esm flag the bundled-output works fine - at least in the scenario form the discourse thread.

@f-f
Copy link
Member

f-f commented Sep 29, 2023

@ad-si could you try bundling this with spago@next to see if the issue is still present?

@f-f
Copy link
Member

f-f commented Oct 14, 2023

I'll close this as I think this is now solved, but let's reopen it if it turns out it's not

@f-f f-f closed this as completed Oct 14, 2023
@ad-si
Copy link
Author

ad-si commented Jan 11, 2024

Yes, works! Thanks for your efforts! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants