local-storage icon indicating copy to clipboard operation
local-storage copied to clipboard

Reference to "global" in code intended to run in the browser

Open notclive opened this issue 2 years ago • 3 comments

When I try and use the local storage hook in a vite project, running in dev mode, I get the following error:

Uncaught ReferenceError: global is not defined

Line 14 of local-storage-events.ts references global.window. As far as I'm aware global isn't a global variable in browser environments.

From this discussion I've gathered that most, but not all, bundlers transform global to window.

Could line 14 be safely rewritten to the following?

    if (typeof window.CustomEvent === 'function') {
      return;
    }

notclive avatar Sep 23 '22 20:09 notclive

this also fails for me for the exact same reason. any tips on how to fix it? using it in a vite project.

bboydflo avatar Dec 18 '22 01:12 bboydflo

I've put the following

  define: {
    global: {
      window: {}
    },
  },

in vite.config.ts to workaround the problem.

notclive avatar Dec 23 '22 16:12 notclive

I've put the following

  define: {
    global: {
      window: {}
    },
  },

in vite.config.ts to workaround the problem.

Sadly, this doesn't fix it for me - it'd be great to see support of Vite in @rehooks/local-storage though as it's been around for quite some time & becoming really popular too 😉

CHE1RON avatar Feb 13 '23 13:02 CHE1RON