next.js: Webpack 5 breaks dynamic wasm import for SSR
What version of Next.js are you using?
10.2.3
What version of Node.js are you using?
14.16.0
What browser are you using?
Chrome
What operating system are you using?
Windows
How are you deploying your application?
Other
Describe the Bug
Using Webpack 5 breaks dynamic import for WASM modules when using SSR.
ENOENT: no such file or directory, open '...\.next\server\static\wasm
I’ve provided a minimal reproducible example here: https://github.com/TimoWilhelm/mre-next-with-webassembly-webpack-5
Expected Behavior
Dynamic import of WASM modules should work for SSR when using Webpack 5.
To Reproduce
Run npm run build
info - Using webpack 5. Reason: future.webpack5 option enabled https://nextjs.org/docs/messages/webpack5
info - Checking validity of types
info - Creating an optimized production build
info - Compiled successfully
info - Collecting page data
[=== ] info - Generating static pages (0/3)
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
Error: ENOENT: no such file or directory, open 'C:\...\with-webassembly\.next\server\static\wasm\1f565eb157746630b627.wasm'
info - Generating static pages (3/3)
Disabling SSR for the dynamic import fixes the issue.
const RustComponent = dynamic({
loader: async () => {
// Import the wasm module
const rustModule = await import('../add.wasm')
// Return a React component that calls the add_one method on the wasm module
return (props) => <div>{rustModule.add_one(props.number)}</div>
},
+ ssr: false,
});
Downgrading the Webpack version to use v4 also fixes the issue.
module.exports = {
future: {
- webpack5: true,
+ webpack5: false,
},
webpack(config) {
- config.experiments = { syncWebAssembly: true };
config.output.webassemblyModuleFilename = "static/wasm/[modulehash].wasm";
return config;
},
...
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 9
- Comments: 17 (8 by maintainers)
Commits related to this issue
- Attempt to use wasm in next (will probably fail to build: vercel/next.js#25852) — committed to alineacms/alinea by benmerckx 2 years ago
- Workaround https://github.com/vercel/next.js/issues/25852 — committed to strawberry-graphql/strawberry.rocks by patrick91 2 years ago
- Try to make wasm import work https://github.com/vercel/next.js/issues/25852 — committed to ahuth/raycast2 by ahuth 7 months ago
- Fix build error: webpack import WASM file fail This seems to be a known behaviour: - https://github.com/vercel/next.js/issues/33215 - https://github.com/vercel/next.js/issues/25852 Build is ok but ... — committed to input-output-hk/mithril by dlachaume 6 months ago
- Fix build error: webpack import WASM file fail This seems to be a known behaviour: - https://github.com/vercel/next.js/issues/33215 - https://github.com/vercel/next.js/issues/25852 Build is ok but ... — committed to input-output-hk/mithril by dlachaume 6 months ago
- Fix build error: webpack import WASM file fail This seems to be a known behaviour: - https://github.com/vercel/next.js/issues/33215 - https://github.com/vercel/next.js/issues/25852 Build is ok but ... — committed to input-output-hk/mithril by dlachaume 6 months ago
- Fix build error: webpack import WASM file fail This seems to be a known behaviour: - https://github.com/vercel/next.js/issues/33215 - https://github.com/vercel/next.js/issues/25852 Build is ok but ... — committed to input-output-hk/mithril by dlachaume 6 months ago
- Fix build errors error: `experiments.layers` must be enabled error: webpack import WASM file fail This seems to be a known behaviour: - https://github.com/vercel/next.js/issues/33215 - https://githu... — committed to input-output-hk/mithril by dlachaume 6 months ago
- Fix build errors error: `experiments.layers` must be enabled error: webpack import WASM file fail This seems to be a known behaviour: - https://github.com/vercel/next.js/issues/33215 - https://githu... — committed to input-output-hk/mithril by dlachaume 6 months ago
- Fix nextjs build for dynamic wasm import https://github.com/vercel/next.js/issues/25852 — committed to chungwu/combat-lander by chungwu 5 months ago
`const CopyPlugin = require(“copy-webpack-plugin”); /** @type {import(‘next’).NextConfig} */ const nextConfig = { webpack: function (config, options) { config.plugins.push(new CopyPlugin({ patterns: [ { from: “public/wasm”, to: “./static/wasm” }, ], })) return config; } }
module.exports = nextConfig `
It resolved my issue related to including wasm file in bundle
Nice idea! I made an webpack plugin to create the symlink, so it doesn’t depend on
cleanDistDir.It seems like the issue is the following line part of the webpack config:
https://github.com/vercel/next.js/blob/1ebf26af784637a27fe422090418126c474353f4/packages/next/build/webpack-config.ts#L918-L921
The server output gets prefixed with
'chunks'which can’t be resolved by the static site generation.I’ve managed to make it work by using the following webassemblyModuleFilename config in my
next.config.jsbut that doesn’t seem ideal.Didn’t worked without
config.optimization.moduleIds = 'named';.So the result workaround for me is:
+
@michalzalobny these are from node.js bult-in modules -
joinis fromnode:pathwhileaccessandsymlinkare fromnode:fs/promises.When I try to push this plugin it breaks and says that It could not find the
join,access, andsymlinkfunctions. Where are they declared in your webpack code?Vercel being serverless might be peculiar about what Node.js APIs you can use (here
node:fs/promisesandnode:path).Usually there’s a solution in configuring Vercel for runtime filesystem emulation, but since this is a build-time issue, it won’t be of any use here.