-
-
Notifications
You must be signed in to change notification settings - Fork 129
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
"Cannot use import statement outside a module" error in dev server with react-error-boundary
#982
Comments
react-error-boundary
The component I am using as a workaround is here - https://gist.github.com/rmarscher/70aa76af4e5f8db26473bf50e220fb99 |
I've been re-reading https://vite.dev/guide/dep-pre-bundling. It seems to be an error with ESM loaded in a CommonJS context. I thought
might be the fix... but that doesn't seem to work. |
Isn't it |
I tried that and also tried ssr external / noExternal but haven't found a configuration that works. |
(I'll try it later...) |
Okay, one technical hurdle is that we can't configure vite config for two vite processes separately. This will be addressed in v0.22.0. But, even if I configure it manually in the waku source code, it doesn't seem to work: diff --git a/packages/waku/src/lib/middleware/dev-server-impl.ts b/packages/waku/src/lib/middleware/dev-server-impl.ts
index 7ce7d139..07225476 100644
--- a/packages/waku/src/lib/middleware/dev-server-impl.ts
+++ b/packages/waku/src/lib/middleware/dev-server-impl.ts
@@ -124,6 +124,9 @@ const createMainViteServer = (
},
ssr: {
external: ['waku'],
+ optimizeDeps: {
+ include: ['react-error-boundary'],
+ },
},
appType: 'mpa',
server: { middlewareMode: true }, It still errors with "SyntaxError: Cannot use import statement outside a module". I'm not sure what's wrong. We need some help from vite experts. Note that this error comes from SSR only. For RSC, it works because of |
Another workaround would be simply wrap (re-export) |
I looked into it a little deeper. It's actually a Waku-related issue. To prove it, this patch fixes the issue: diff --git a/packages/waku/src/lib/middleware/dev-server-impl.ts b/packages/waku/src/lib/middleware/dev-server-impl.ts
index 7ce7d139..d9be9ee2 100644
--- a/packages/waku/src/lib/middleware/dev-server-impl.ts
+++ b/packages/waku/src/lib/middleware/dev-server-impl.ts
@@ -135,6 +135,12 @@ const createMainViteServer = (
const loadServerModuleMain = async (idOrFileURL: string) => {
const vite = await vitePromise;
+ if (
+ idOrFileURL ===
+ 'file://node_modules/react-error-boundary/dist/react-error-boundary.development.esm.js'
+ ) {
+ return vite.ssrLoadModule('react-error-boundary');
+ }
if (idOrFileURL === 'waku' || idOrFileURL.startsWith('waku/')) {
// HACK `external: ['waku']` doesn't do the same
return import(/* @vite-ignore */ idOrFileURL); So, the issue is file resolving issue with |
Another way is this: diff --git a/packages/waku/src/lib/middleware/dev-server-impl.ts b/packages/waku/src/lib/middleware/dev-server-impl.ts
index 7ce7d139..f87e370b 100644
--- a/packages/waku/src/lib/middleware/dev-server-impl.ts
+++ b/packages/waku/src/lib/middleware/dev-server-impl.ts
@@ -286,6 +286,10 @@ const createRscViteServer = (
config: { rootDir: string; basePath: string },
initialModules: ClonableModuleNode[],
) => {
+ id = id.replace(
+ '/node_modules/react-error-boundary/dist/react-error-boundary.development.esm.js',
+ '/node_modules/.vite/waku-dev-server-main/deps/react-error-boundary.js',
+ );
let file = id.startsWith('file://')
? decodeFilePathFromAbsolute(fileURLToFilePath(id))
: id; This is related with |
The issue can actually be in rscTransform plugin. I'm working on it. |
I tried adding
react-error-boundary
to my app and it gives an error with the dev server. I think it works with the production build. I recreated my own ErrorBoundary class component with a "use client" directive by copying from that library and it works in dev mode.Related to #423. The
react-error-boundary
package is still recommended in the React 19 docs at the bottom of this section and in some other code examples https://19.react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundaryTo reproduce, create a waku app, add
react-error-boundary
dependency, import and use it somewhere (like App.tsx)Here is the error:
The text was updated successfully, but these errors were encountered: