Skip to content
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

Error - Window is not defined #1207

Closed
insikdev opened this issue Feb 7, 2023 · 5 comments
Closed

Error - Window is not defined #1207

insikdev opened this issue Feb 7, 2023 · 5 comments

Comments

@insikdev
Copy link

insikdev commented Feb 7, 2023

find same issues about this error ( #1179 , #1101 ).

I can't build next.js proejct and can't refresh page in dev server, but found a solution using dynamic import.

This error is because window object is not defined in non-browser environments.

const win = window as Window & InjectedWindow;

Using globlaThis instead of a window object did not cause the same error.

const win = globalThis as Window & InjectedWindow;

After the fix, the error no longer occurred in my environment. ( next.js 13.1.6 )

@jacogr
Copy link
Member

jacogr commented Feb 7, 2023

The error is correct - the code is meant to fully execute in a browser environment, not on a server.

@jacogr jacogr added the support label Feb 7, 2023
@yashirooooo
Copy link

The error is correct - the code is meant to fully execute in a browser environment, not on a server.

I am running in react environment. but also reported such an error

@jacogr
Copy link
Member

jacogr commented Feb 8, 2023

Any browser has "browser" defined.

  1. Open a new browser windo
  2. Open the console
  3. type console.log(window)

The output would be similar to this -

image

This means that if you get this error, your script is not executing in the browser context. In that case, it won't work - @polkadot/extension-dapp interrogates the window object to determine what is injected.

So if you use next.js or any other environment where the JS does not execute on the browser client, but rather the server and then served to the client, your best approach would be to use something like dynamic imports to force the context.

@polkadot-js-bot
Copy link

This issue has been open for 21 days with no activity and is not labelled as an enhancement. It will be closed in 7 days.

@polkadot-js-bot
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Mar 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants