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

WebpackError: ReferenceError: window is not defined when trying to use darkreader api for dark mode on my website. #1624

Closed
rsapkf opened this issue Oct 11, 2019 · 7 comments

Comments

@rsapkf
Copy link

rsapkf commented Oct 11, 2019

I'm trying to implement darkreader's dark mode by default on my gatsby site but the build fails with "window is not defined" error. I tried almost everything from https://www.gatsbyjs.org/docs/debugging-html-builds/ and gatsbyjs/gatsby#309 but still no luck.

@alexanderby
Copy link
Member

Unfortunately I don't know how the Gatsby works. Dark Reader script should run in browser, but not in Node.js environment.

@Gusted
Copy link
Contributor

Gusted commented Jul 24, 2020

Use it as a NPM package.

@Gusted Gusted closed this as completed Jul 24, 2020
@ShahriarKh
Copy link

ShahriarKh commented May 22, 2021

I'm using Next.js and have the same problem :/
I installed the npm package, and also tried importing modules in index.js, _app.js, _document.js, my-component.js but no luck.

@Gusted
Copy link
Contributor

Gusted commented May 23, 2021

Open up a new issue.

@jayasio
Copy link

jayasio commented May 31, 2021

@ShahriarKh +others looking for this, this is not a bug in darkreader.

In Next.js you should import client side libraries with "dynamic import"
https://nextjs.org/docs/advanced-features/dynamic-import
https://stackoverflow.com/questions/52939439/dynamic-import-node-module-with-next-js

This might be something similar in Gatsby.
https://www.gatsbyjs.com/docs/using-client-side-only-packages/

@Jeidnx
Copy link

Jeidnx commented Mar 14, 2022

For all persons looking at this: There is no good solution.

It is correct that this is not a bug in darkreader, however darkreader are the only ones who can properly fix it by not throwing an error if no window is present.

Dynamically importing Libraries in NextJS is not supported and doesn't work (anymore?).
vercel/next.js#13417 (comment)

In the meantime I have a solution that is working for me:

const [DarkReader, setDarkReader] = useState({});

useEffect(() => {
    import("darkreader").then((lib) => {
        setDarkReader(lib)
    })
}, [])

// Remember to check if library has loaded
if(typeof DarkReader.isEnabled === 'undefined'){
    return(<h1>Loading...</h1>)
}

@ShahriarKh
Copy link

ShahriarKh commented Mar 14, 2022

This is how I managed to fix it in Next.js:

const darkReaderOptions = { brightness: 100, contrast: 96, sepia: 0 };

export async function toggleDarkMode() {
   if (typeof window != "undefined") {
      const { isEnabled, enable, disable, setFetchMethod } = await import("darkreader");
      setFetchMethod(window.fetch);
      const isOn = isEnabled();
      isOn ? disable() : enable(darkReaderOptions);
   }
}
<button onClick={toggleDarkMode} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants