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

Webpack: Can't resolve 'fs' #4

Open
tessereth opened this issue Aug 6, 2019 · 1 comment
Open

Webpack: Can't resolve 'fs' #4

tessereth opened this issue Aug 6, 2019 · 1 comment

Comments

@tessereth
Copy link
Contributor

I tried pulling in this npm package and compiling it with webpack but I get the following errors:

ERROR in ./node_modules/micropython/index.js
Module not found: Error: Can't resolve 'fs' in './micropython_example/node_modules/micropython'
 @ ./node_modules/micropython/index.js 21:9-22
 @ ./assets/js/app.js
 @ ./assets/js/index.js

ERROR in ./node_modules/micropython/lib/micropython.js
Module not found: Error: Can't resolve 'fs' in './micropython_example/node_modules/micropython/lib'
 @ ./node_modules/micropython/lib/micropython.js 39:15-28 80:13-26 179:28-41 3005:23-36
 @ ./node_modules/micropython/index.js
 @ ./assets/js/app.js
 @ ./assets/js/index.js

This appears to be because webpack is not detecting that it can never go down the "node-only" path and still trying to require fs. If I add typeof __webpack_require__ !== 'function' on every branch that requires fs, everything works. But I don't know enough about webpack to be confident such changes are a good idea and/or they wouldn't break it for someone else.

You can see my webpack version and config and a working example at https://github.com/tessereth/micropython_wasm_example.

@tessereth
Copy link
Contributor Author

Reading through various issues (including webpack/webpack#7352), looks like you can get around issues in the emscripten generated javascript (lib/micropython.js) by adding an ignore plugin to the webpack config:

  plugins: [
    ...
    new webpack.IgnorePlugin(/^fs$/),
  ],

This does not fix the issue in index.js. I'm pretty sure this code:

if (typeof webpackJsonp !== 'object') {
pyjs = require('fs').readFileSync(__dirname + '/js.py').toString();
}

does not work as intended. When running in the brower, webpackJsonp is undefined even when it was originally built with webpack. Perhaps using typeof window === 'undefined' would be better?

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

1 participant