Home > OS >  const causing "window is undefined error" but fixing that leaves it out of scope
const causing "window is undefined error" but fixing that leaves it out of scope

Time:08-25

I'm trying to initialize a const, but doing so gives me a window is undefined error in nextjs. Fixing that with:

if (typeof window !== "undefined") {
  const web3Modal = new Web3Modal({
  network: "mainnet",
  cacheProvider: true,
  providerOptions,
});
}

Causes the const to be out of scope from the rest of my code. How would I go around this?

CodePudding user response:

Can use let instead of const.

let web3Modal;
if (typeof window !== "undefined") {
  web3Modal = new Web3Modal({
  network: "mainnet",
  cacheProvider: true,
  providerOptions,
});
}
  • Related