Reference Error window is not defined when importing Provider in nextjs
See original GitHub issueIssue and Steps to Reproduce
- Create a Nextjs application.
- add @axa-fr/react-oidc-context package
- import provider in _app.js
import { AuthenticationProvider } from "@axa-fr/react-oidc-context";
- Congratulations, you have the error
The issue take place in withRouter
and since it uses window
even a simple import of Provider causes reference error since window is undefined
on node server.
ReferenceError: window is not defined
at Object.<anonymous> (/Users/ramin/Documents/Sonbol/next-ids4/node_modules/@axa-fr/react-oidc-core/dist/routes/withRouter.js:46:38)
at Module._compile (node:internal/modules/cjs/loader:1083:30)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
at Module.load (node:internal/modules/cjs/loader:948:32)
at Function.Module._load (node:internal/modules/cjs/loader:789:14)
at Module.require (node:internal/modules/cjs/loader:972:19)
at require (node:internal/modules/cjs/helpers:88:18)
at Object.<anonymous> (/Users/ramin/Documents/Sonbol/next-ids4/node_modules/@axa-fr/react-oidc-core/dist/default-component/SessionLost.component.js:7:36)
at Module._compile (node:internal/modules/cjs/loader:1083:30)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
error - ReferenceError: window is not defined
at Object.<anonymous> (/Users/ramin/Documents/Sonbol/next-ids4/node_modules/@axa-fr/react-oidc-core/dist/routes/withRouter.js:46:38)
at Module._compile (node:internal/modules/cjs/loader:1083:30)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
at Module.load (node:internal/modules/cjs/loader:948:32)
at Function.Module._load (node:internal/modules/cjs/loader:789:14)
at Module.require (node:internal/modules/cjs/loader:972:19)
at require (node:internal/modules/cjs/helpers:88:18)
at Object.<anonymous> (/Users/ramin/Documents/Sonbol/next-ids4/node_modules/@axa-fr/react-oidc-core/dist/default-component/SessionLost.component.js:7:36)
at Module._compile (node:internal/modules/cjs/loader:1083:30)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
ReferenceError: window is not defined
at Object.<anonymous> (/Users/ramin/Documents/Sonbol/next-ids4/node_modules/@axa-fr/react-oidc-core/dist/routes/withRouter.js:46:38)
at Module._compile (node:internal/modules/cjs/loader:1083:30)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
at Module.load (node:internal/modules/cjs/loader:948:32)
at Function.Module._load (node:internal/modules/cjs/loader:789:14)
at Module.require (node:internal/modules/cjs/loader:972:19)
at require (node:internal/modules/cjs/helpers:88:18)
at Object.<anonymous> (/Users/ramin/Documents/Sonbol/next-ids4/node_modules/@axa-fr/react-oidc-core/dist/default-component/SessionLost.component.js:7:36)
at Module._compile (node:internal/modules/cjs/loader:1083:30)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
Additional Details
- Installed packages:
@axa-fr/react-oidc-context
oidc-client
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Window is not defined in Next.js React app - Stack Overflow
The error occurs because window is not yet available, while component is still mounting. You can access window object after ...
Read more >How to solve Next.js window is not defined
ReferenceError : window is not defined is a pretty common error you may run into when using Next.js for the first time but...
Read more >How to solve "window is not defined" errors in React and Next.js
How to solve "window is not defined" errors in React and Next.js · 1. First solution: typeof · 2. Second solution: the useEffect...
Read more >How to Fix "window is not defined" in Next.js - Upmostly
This issue has to do with Server-Side Rendering in Next.js. Next.js will by default try to use SSR for your site. This means...
Read more >How to fix window is not defined error in Next.js
The very obvious fix would be to check if the window object exists or not, and access the window only if it exists....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think i will build a full demo with nextjs. It will help to maintain the compatibility and improve the documentation.
I found a work around for now, you can use next/dynamic to load dynamically with SSR diasbled, so create a component like:
then import to your app.jsx/tsx like:
Then you can wrap anything the same way you would if you were using
<AuthenticationProvider>
. Seems like this has to be done with pretty much everything from this lib when using it in a nextJs app unfortunately