Skip to content

String.raw breaks module loading with Nodejs and Webpack #2605

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

Closed
Tarnadas opened this issue Jun 30, 2021 · 0 comments · Fixed by #2606
Closed

String.raw breaks module loading with Nodejs and Webpack #2605

Tarnadas opened this issue Jun 30, 2021 · 0 comments · Fixed by #2606
Labels

Comments

@Tarnadas
Copy link
Contributor

Tarnadas commented Jun 30, 2021

When trying to bundle a WebAssembly module generated from wasm-bindgen via Webpack 5, Node fails to load internal modules (the ones that are provided by Node like util, fs, etc).
I encountered this problem due to the TextEncoder and TextDecoder imports from util.
wasm-bindgen generated this code in my JS file:

const { TextDecoder, TextEncoder } = require(String.raw`util`);

Bundling this generated code via Webpack 5 and running the bundle with Node 16 resulted in the error:

Error: Cannot find module 'util'
    at t (/home/marior/projects/react-wasm/examples/ssr/dist/server/860.index.js:1:68648)
    at eval (webpack-internal:///860:7:62)
    at Object.860 (/home/marior/projects/react-wasm/examples/ssr/dist/server/860.index.js:1:111)
    at __webpack_require__ (/home/marior/projects/react-wasm/examples/ssr/dist/server/index.js:1:8451)
    at Function.__webpack_require__.t (/home/marior/projects/react-wasm/examples/ssr/dist/server/index.js:1:8662) {
  code: 'MODULE_NOT_FOUND'
}

Manually removing the String.raw from the import fixed it for me.

I assume the String.raw prevents OS compat issues? I have also other imports from JS code snippets that are properly loaded with String.raw, so I think this error just occurs on internal Node modules.

I will send a PR for this to be fixed.

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

Successfully merging a pull request may close this issue.

1 participant