'Window is not defined Vite Vitesse

I'm having a problem trying to build my application. When running in dev mode, it works perfectly, but when I build it it gives window is not defined.

enter image description here

export function socketConnect (): SocketI {
  if (typeof window.gqlsocket === 'undefined') {
    const socket = initializeSocket()
    window.gqlsocket = socket
    return socket
  } else {
    return window.gqlsocket
  }
}

export function socketDisconnect (): void {
  if (typeof window.gqlsocket !== 'undefined') {
    window.gqlsocket.disconnect(() => (window.gqlsocket = undefined))
  }
}

const client = createClient({...})

export const install: ModuleInstall = ({ app, isClient }) => {
  if (!isClient) return

  app.use(urql, client)
}


Solution 1:[1]

Add to the index.html :

<div id="app"></div>
<script type="module" src="/src/main.ts"></script>

<!-- ? -->
<script>
  var global = global || window;
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1