waku: Mantine Component Library / dev mode / Error when evaluating SSR module
How can I use Mantine with WAKU without getting this Error:
(waku dev --with-ssr or waku dev - no problem with waku build --with-ssr)
repo to replicate the error: https://github.com/aheissenberger/waku-mantine-broken
22:27:44 [vite] Error when evaluating SSR module /node_modules/.pnpm/use-callback-ref@1.3.1_@types+react@18.2.48_react@18.3.0-canary-b30030471-20240117/node_modules/use-callback-ref/dist/es2015/useRef.js: failed to import "react"
|- SyntaxError: [vite] Named export 'useState' not found. The requested module 'react' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'react';
const {useState} = pkg;
at analyzeImportedModDifference (file:///waku-mantine/node_modules/.pnpm/vite@5.0.12/node_modules/vite/dist/node/chunks/dep-9A4-l-43.js:50953:19)
at nodeImport (file:///waku-mantine/node_modules/.pnpm/vite@5.0.12/node_modules/vite/dist/node/chunks/dep-9A4-l-43.js:50904:9)
at async ssrImport (file:///waku-mantine/node_modules/.pnpm/vite@5.0.12/node_modules/vite/dist/node/chunks/dep-9A4-l-43.js:50799:24)
at async eval (/waku-mantine/node_modules/.pnpm/use-callback-ref@1.3.1_@types+react@18.2.48_react@18.3.0-canary-b30030471-20240117/node_modules/use-callback-ref/dist/es2015/useRef.js:3:44)
at async instantiateModule (file:///waku-mantine/node_modules/.pnpm/vite@5.0.12/node_modules/vite/dist/node/chunks/dep-9A4-l-43.js:50861:9)
Cannot render RSC Error: SyntaxError: [vite] Named export 'useState' not found. The requested module 'react' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'react';
const {useState} = pkg;
at file:///waku-mantine/node_modules/.pnpm/file+..+..+DEV+waku+packages+waku+waku-0.19.1.tgz_react-dom@18.3.0-canary-b30030471-20240117__jadhby4bmjtn267th77iortg3m/node_modules/waku/dist/lib/handlers/dev-worker-api.js:148:60
at Worker.<anonymous> (file:///waku-mantine/node_modules/.pnpm/file+..+..+DEV+waku+packages+waku+waku-0.19.1.tgz_react-dom@18.3.0-canary-b30030471-20240117__jadhby4bmjtn267th77iortg3m/node_modules/waku/dist/lib/handlers/dev-worker-api.js:33:52)
at Worker.emit (node:events:531:35)
at MessagePort.<anonymous> (node:internal/worker:263:53)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:822:20)
at exports.emitMessage (node:internal/per_context/messageport:23:28)
Versions:
Mantine: 7.4.2 - works with NextJS App Router and has 'use client' in all components - https://mantine.dev/guides/next/
WAKU: 0.19.1
root-layout.tsx
import '@mantine/core/styles.css';
import { ColorSchemeScript, createTheme, MantineProvider } from '@mantine/core';
import type { ReactNode } from 'react';
import { Header } from '../components/header.js';
import { Footer } from '../components/footer.js';
const theme = createTheme({
/** Put your mantine theme override here */
});
type RootLayoutProps = { children: ReactNode };
export const RootLayout = async ({ children }: RootLayoutProps) => {
const data = await getData();
return (
<div id="__waku" >
<meta property="description" content={data.description} />
<link rel="icon" type="image/png" href={data.icon} />
<ColorSchemeScript />
<MantineProvider theme={theme}>
<Header />
<main >
{children}
</main>
<Footer />
</MantineProvider>
</div>
);
};
const getData = async () => {
const data = {
description: 'An internet website!',
icon: '/images/favicon.png',
};
return data;
};
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 85 (72 by maintainers)
Commits related to this issue
- fix(website): hack next-mdx-remote with vite config (DEV only) (#779) Hmm, this is going to be a big problem with ESM/CJS compat... Maybe related: #421, #428 — committed to wakujs/waku by dai-shi 2 years ago
- fix(website): hack next-mdx-remote with vite config (DEV only) (#779) Hmm, this is going to be a big problem with ESM/CJS compat... Maybe related: #421, #428 — committed to himself65/waku by dai-shi 2 years ago
@dai-shi @Aslemammad - you rock!
WAKU git commit ff20bfe538e32d9315f679a5d4d40e0b377aee18 fixed all problems for
build --with-ssrandstart --with-ssrand works without avite.config.tsfile - even the mantine chart library now works pnpm waku build --with-ssr 👍 pnpm waku start --with-ssr 👍Update on DEV MODE problems: pnpm waku dev 👎 pnpm waku dev --with-ssr 👎
Except for the Mantine Charts which fails with an additional Error I can fix the problems in DEV MODE by adding a
vite.config.tswith some special treatments for the libraries. But it would be great to solve this too.Error in DEV MODE: - without ssr the error is thrown in the browser.
I can do that in the next few days!
still breaks with
@mantine/dateseven when added tovite.config.tsFollow up:
I’m happy I managed to fix this for my own use-case but I think it’s far more important we use this opportunity to nail down what the root cause is. We all know from first-hand experience that debugging issues with Vite’s bundling of 3rd-party deps can be a nightmare. So let’s try and understand once and for all how each of Vite’s configuration options affects the bundling process, rather than blindly tweaking things until it works
Before & After Comparison
“Broken” setup
vite.config.ts
Contents of
node_modules > .vite > waku-dev-serverinspected from browser sources:Working setup
vite.config.ts
Contents of
node_modules > .vite > waku-dev-serverinspected from browser sources:Observations & open questions
This is observed by adding
@mantine/coretossr.noExternal, which fixes the missing context provider error on the server and allows the initial SSRd HTML to render in the browser. However, during hydration in the browser, the client crashes as it re-executes the same logic but it’s not using the corrected module graph used on the server.The answer to this is adding
@mantine/coretooptimizeDeps.exclude, which seems to instruct Vite to also leave Mantine and its dependencies in their raw unbundled ESM format on the client.We’ve already suspected that the “missing context provider” triggered by Mantine is caused by multiple versions of react and/or @mantine/core in the module graph. The before & after screenshots confirm this.
Why is the default behaviour of Waku/Vite in this scenario to include
@mantine/coretwice in the module graph? This can clearly be seen in the “before” screenshot where @mantine is both in Vite’s optimised deps underwaku-dev-serveras well as the root node_modules. This double-inclusion is the root cause and we need to figure out: is this a Waku-specific problem? Vite-specific? Is it related to React v19 rc being used? Is there a problem with Mantine itself?SOLVED IT! FINALLY.
Just when this entire thing seemed impossible to understand and I was about to give up… Here’s the required config, I will post an explanation of my findings later:
npm i, add this to your package.json (note that I’ve only tried this with npm so far, not pnpm):Also note that you don’t have to put this on your package.json. You can instead add the
--legacy-peer-depsflag when runningnpm i. I personally find it slightly less hacky to add the overrides.vite.config.tsfile with the following:Mantine should work fine in dev mode now
Updated Status with WAKU
0.21.0-beta.0Commit 8ffc4f546b6ad52638cc930f6ba192823b0ca66dErrors with
waku dev:with this in
vite.config.ts:the error changes to:
I am not able to fix the problem and need help @dai-shi @Aslemammad:
I was able to replicate the problem with a simple replication of the
MantineProviderand was able to track down the problem to be related to the bundling vitejs is doing for external packages in DEV mode. It could be related to the problem, that the package is referencing a different react version but there is only one version installed in the node_modules folder.Here is a reference project which shows the problem: https://github.com/aheissenberger/waku-mantine-broken-01
pnpm waku dev- navigate to link MyUI Package (broken) and look at the console. I replaced the Exception with a console.log to keep the react tree. https://github.com/aheissenberger/myui/blob/5715f4ea793fd2662e54acc4f7a0e192d9fb88da/src/MyThemeProvider.mjs#L12I had to add the provider as an external package to get the error: https://github.com/aheissenberger/myui
The same provider local as part of a WAKU project will not loose the context: https://github.com/aheissenberger/waku-mantine-broken-01/blob/b1c25538e7d6e1febd3ec94c205b292ba09c5814/src/components/my/MyThemeProvider.tsx#L11
We haven’t decided the docs structure, so let’s start with something like
./docs/ui-libs/mantine.mdx. We will probably re-structure it eventually.This fixed it.
While investigating, found out that MantineProvider was importing the ESM format of mantine (all modules were seperate), but the CopyButton was receiving the optimized bundled version of mantine (a huge mantine_core.js file, maybe cjs?).
I’m not sure about the reason, but I guess this is either a vite issue, because this happened when the module was being imported later on (e.g. dynamic import through chunk loading in waku). Or it’s a package exports issue from mantine. Because MantineThemeProvider was being imported first as ESM and later, the CopyButton was being imported with a dynamic import, as cjs.
I first doubted that waku was importing modules in two different modes (one main thread, one worker) in the server, but that was not correct, plus this issue was also happening in the client (that’s why we have
optimizeDepsthere)Feel free to close this issue if it resolved the problem.
I have no idea how Next.js works but I use Solid Start which uses vinxi from @nksaraf. The internal output folder
.vinxicontains three different bundles .I did some research based on the official Vite SSR & SSG Example code and my findings are:
Mantine Component Libraryhas no problems with SSR and SSG in nodeHere is my repro which I used to conduct the tests: https://github.com/aheissenberger/vite-ssr-test
I suggest to change the build pipeline to support the following target output:
maybe have a look at vinxi - specifically the existing deploy adapter from the Nuxt ecosystems Nitro Stack are amazing.