I added a dependency to my nextjs project that pulls in another dependency that uses wasm, and I am hitting a wall with the webpack build errors I am getting.
I tried a bunch of different things with the nextjs webpack config, but they all gave the same error as the second error message I have attached.
Using the default nextjs configuration:
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially useful
* for Docker builds.
*/
import "./src/env.js";
/** @type {import("next").NextConfig} */
const config = {};
export default config;
~/../4nswer.me ❯ pnpm build
> [email protected] build /home/riley/programming/4nswer.me
> next build
▲ Next.js 15.3.2
- Environments: .env
Creating an optimized production build ...
⚠ Compiled with warnings in 11.0s
./node_modules/.pnpm/[email protected]/node_modules/tiktoken/lite/tiktoken_bg.wasm
The generated code contains 'async/await' because this module is using "asyncWebAssembly".
However, your target environment does not appear to support 'async/await'.
As a result, the code may not run as expected or may cause runtime errors.
Import trace for requested module:
./node_modules/.pnpm/[email protected]/node_modules/tiktoken/lite/tiktoken_bg.wasm
./node_modules/.pnpm/[email protected]/node_modules/tiktoken/lite/tiktoken.js
./node_modules/.pnpm/@[email protected]/node_modules/@anthropic-ai/tokenizer/dist/cjs/index.js
./node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@effect+platfo_37ce30aee9d8a070baae66b1689224a0/node_modules/@effect/ai-anthropic/dist/esm/AnthropicTokenizer.js
./node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@effect+platfo_37ce30aee9d8a070baae66b1689224a0/node_modules/@effect/ai-anthropic/dist/esm/index.js
./src/types/types.ts
./src/components/MainArea.tsx
./src/app/page.tsx
✓ Linting and checking validity of types
Collecting page data ...Error: Missing tiktoken_bg.wasm
at 4221 (.next/server/app/api/rpc/route.js:14:1100663)
at t (.next/server/webpack-runtime.js:1:143)
at 3820 (.next/server/app/api/rpc/route.js:1:703030)
at t (.next/server/webpack-runtime.js:1:143)
at 4203 (.next/server/app/api/rpc/route.js:14:1089870)
at t (.next/server/webpack-runtime.js:1:143)
at a (.next/server/app/api/rpc/route.js:14:1101349)
at <unknown> (.next/server/app/api/rpc/route.js:14:1101376)
at t.X (.next/server/webpack-runtime.js:1:1275)
at <unknown> (.next/server/app/api/rpc/route.js:14:1101362)
> Build error occurred
[Error: Failed to collect page data for /api/rpc] { type: 'Error' }
ELIFECYCLE Command failed with exit code 1.
~/../4nswer.me ❯
Updating my webpack config like it suggests:
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially useful
* for Docker builds.
*/
import "./src/env.js";
/** @type {import("next").NextConfig} */
const config = {
webpack: (config) => {
config.experiments = {
...config.experiments,
asyncWebAssembly: true,
};
return config;
},
};
export default config;
~/../4nswer.me ❯ pnpm build
> [email protected] build /home/riley/programming/4nswer.me
> next build
▲ Next.js 15.3.2
- Environments: .env
Creating an optimized production build ...
Failed to compile.
./node_modules/.pnpm/[email protected]/node_modules/tiktoken/lite/tiktoken_bg.wasm
Module parse failed: Unexpected character '' (1:0)
The module seem to be a WebAssembly module, but module is not flagged as WebAssembly module for webpack.
BREAKING CHANGE: Since webpack 5 WebAssembly is not enabled by default and flagged as experimental feature.
You need to enable one of the WebAssembly experiments via 'experiments.asyncWebAssembly: true' (based on async modules) or 'experiments.syncWebAssembly: true' (like webpack 4, deprecated).
For files that transpile to WebAssembly, make sure to set the module type in the 'module.rules' section of the config (e. g. 'type: "webassembly/async"').
(Source code omitted for this binary file)
Import trace for requested module:
./node_modules/.pnpm/[email protected]/node_modules/tiktoken/lite/tiktoken_bg.wasm
./node_modules/.pnpm/[email protected]/node_modules/tiktoken/lite/tiktoken.js
./node_modules/.pnpm/@[email protected]/node_modules/@anthropic-ai/tokenizer/dist/cjs/index.js
./node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@effect+platfo_37ce30aee9d8a070baae66b1689224a0/node_modules/@effect/ai-anthropic/dist/esm/AnthropicTokenizer.js
./node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@effect+platfo_37ce30aee9d8a070baae66b1689224a0/node_modules/@effect/ai-anthropic/dist/esm/index.js
./src/types/types.ts
./src/components/MainArea.tsx
./src/app/page.tsx
> Build failed because of webpack errors
ELIFECYCLE Command failed with exit code 1.
~/../4nswer.me ❯