Window is not defined Vite Vitesse

Viewed 2311

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)
}
1 Answers

Add to the index.html :

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

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