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
Comments
Unfortunately I don't know how the Gatsby works. Dark Reader script should run in browser, but not in Node.js environment. |
Use it as a NPM package. |
I'm using Next.js and have the same problem :/ |
Open up a new issue. |
@ShahriarKh +others looking for this, this is not a bug in darkreader. In Next.js you should import client side libraries with "dynamic import" This might be something similar in Gatsby. |
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?). In the meantime I have a solution that is working for me:
|
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} /> |
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.
The text was updated successfully, but these errors were encountered: