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

Uncaught ReferenceError: window is not defined #182

Open
zm1994 opened this issue Jan 7, 2019 · 3 comments
Open

Uncaught ReferenceError: window is not defined #182

zm1994 opened this issue Jan 7, 2019 · 3 comments
Labels
bug-major Bugs that are affecting production users.

Comments

@zm1994
Copy link

zm1994 commented Jan 7, 2019

I'm getting error when use this library on browser.
Full error: Uncaught ReferenceError: window is not defined at Module../node_modules/current-device/es/index.js (index.js:4)

From the code of library - something is wrong with:
"var previousDevice = window.device;"

My code:
const device = require('current-device').default;
const isMobile = function () {
return device.mobile();
};

I use webpack 4 for building.

@Blitzlord
Copy link

Blitzlord commented Jan 21, 2019

An option to set the window object would be nice. e.g.:
device(window) or device(customWindow)

@neophob
Copy link

neophob commented Mar 27, 2019

I use ava and browser-env to test my frontend app. However this seems not to work as it fails:

var previousDevice = window.device;
                     ^

ReferenceError: window is not defined
    at Object.<anonymous> (/foo/node_modules/current-device/lib/index.js:8:22)

It looks like code is executed as soon as the import of current-device has been made

@matthewhudson matthewhudson added bug-major Bugs that are affecting production users. Difficulty: Beginner labels Aug 20, 2019
@asheabbott
Copy link

In case anyone is here looking for potential solutions, I was able to import the package without the window error in Next.js like this:

// _app.js (Next.js)

// Whatever initial code here

const MyApp = ({ Component, pageProps }) => {
  // Whatever code here
  
  const currentDevice = async () => {
    await import("current-device");
  };

  useEffect(() => {
    currentDevice();
  }, []);

  // Whatever other code here

};

export default MyApp;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-major Bugs that are affecting production users.
Projects
None yet
Development

No branches or pull requests

5 participants