I am currently trying to integrate cornerstone into an nextjs react app. Since there is sadly no tutorial, I am struggling quite early. Below I will provide a minimal example for easy reproduction.
- Setup a nextjs react app
npx create-next-app@latest
(default settings for everything) cd my-app
- Start the development server
npm run dev
and navigate tolocalhost:3000
in the browser to see that nextjs is running. - Install cornerstone according to Installation | Cornerstone.js
npm install @cornerstonejs/core @cornerstonejs/tools @cornerstonejs/streaming-image-volume-loader
- Add the following line to the top of the file
src/app/page.tsx
import * as cornerstoneTools from "@cornerstonejs/tools";
Observe the error
./node_modules/@icr/polyseg-wasm/dist/ICRPolySeg.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)
There is already a thread about this bug, but the solution is using vite as build tool.
Nextjs is using webpack, so a vite solution is sadly not viable.
Modify the next.config.mjs to include the webassembly flags from the error messages:
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack(config) {
// config.experiments.syncWebAssembly = true;
config.experiments.asyncWebAssembly = true;
return config;
},
};
export default nextConfig;
This leads to the following error
./node_modules/@icr/polyseg-wasm/dist/ICRPolySeg.wasm
Module not found: Can't resolve 'a'
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
./node_modules/@icr/polyseg-wasm/dist/index.js
./node_modules/@cornerstonejs/tools/dist/esm/workers/polySegConverters.js
./node_modules/@cornerstonejs/tools/dist/esm/stateManagement/segmentation/polySeg/registerPolySegWorker.js
./node_modules/@cornerstonejs/tools/dist/esm/stateManagement/segmentation/polySeg/computeAndAddRepresentation.js
./node_modules/@cornerstonejs/tools/dist/esm/stateManagement/segmentation/polySeg/Surface/computeAndAddSurfaceRepresentation.js
./node_modules/@cornerstonejs/tools/dist/esm/stateManagement/segmentation/polySeg/index.js
./node_modules/@cornerstonejs/tools/dist/esm/stateManagement/segmentation/index.js
./node_modules/@cornerstonejs/tools/dist/esm/index.js
./src/app/page.tsx
Does anyone know how to solve this problem?
Is there a tutorial on how to integrate cornerstone and react that I missed?
thank you
Alex