color-mode
color-mode copied to clipboard
helper.getColorScheme is not a function error when trying to use the module
Version
@nuxtjs/color-mode: nuxt:
Reproduction Link
https://github.com/maxacarvalho/nuxtjs-color-mode
Steps to reproduce
-
npm install
-
npm run dev
- Access the local URL http://localhost:3000/
- Try to select one of the color modes
- Refresh the page
What is Expected?
I expect to be able to switch between color modes
What is actually happening?
Nothing happens at first and then a 500 error is thrown
I also got the same error
Hi.
In the browser, go to localstorage, find nuxt-color-mode, delete the line. Reload the page.
This issue also appears when testing a nuxt app.
During testing, the error originates from here: https://github.com/nuxt-modules/color-mode/blob/0d0e09702ade91f59fc69a82d2da780a2e6cd7db/src/runtime/plugin.client.ts#L8-L14
(window[globalName] || {}).removeColorScheme
is undefined:
https://github.com/nuxt-modules/color-mode/blob/0d0e09702ade91f59fc69a82d2da780a2e6cd7db/src/runtime/plugin.client.ts#L91
This issue also appears when testing a nuxt app.
During testing, the error originates from here:
https://github.com/nuxt-modules/color-mode/blob/0d0e09702ade91f59fc69a82d2da780a2e6cd7db/src/runtime/plugin.client.ts#L8-L14
(window[globalName] || {}).removeColorScheme
is undefined:https://github.com/nuxt-modules/color-mode/blob/0d0e09702ade91f59fc69a82d2da780a2e6cd7db/src/runtime/plugin.client.ts#L91
Did you find a fix for this error, I got the same while running a test?
This issue also appears when testing a nuxt app. During testing, the error originates from here: https://github.com/nuxt-modules/color-mode/blob/0d0e09702ade91f59fc69a82d2da780a2e6cd7db/src/runtime/plugin.client.ts#L8-L14
(window[globalName] || {}).removeColorScheme
is undefined: https://github.com/nuxt-modules/color-mode/blob/0d0e09702ade91f59fc69a82d2da780a2e6cd7db/src/runtime/plugin.client.ts#L91Did you find a fix for this error, I got the same while running a test?
Kinda of, using pnpm patch
:
- https://pnpm.io/cli/patch
In your repo, run pnpm patch @nuxtjs/[email protected]
Follow the path pnpm gives to you, then edit dist/runtime/plugin.client.mjs
L67-68 as such:
helper?.removeColorScheme(oldValue);
helper?.addColorScheme(newValue);
Go back to your repo and run pnpm patch-commit <temp-pnpm-path>
You should have a file named patches/@[email protected]
Commit that, and your CI should now work as usual
My patch file, for comparison:
diff --git a/dist/runtime/plugin.client.mjs b/dist/runtime/plugin.client.mjs
index e4fb18d41122ab0fa842faa69e12b438a0c4b0b3..0ee8a88e39332af03bc5ae9a0f382cc545664341 100644
--- a/dist/runtime/plugin.client.mjs
+++ b/dist/runtime/plugin.client.mjs
@@ -64,8 +64,8 @@ export default defineNuxtPlugin((nuxtApp) => {
window.localStorage?.setItem(storageKey, preference);
}, { immediate: true });
watch(() => colorMode.value, (newValue, oldValue) => {
- helper.removeColorScheme(oldValue);
- helper.addColorScheme(newValue);
+ helper?.removeColorScheme(oldValue);
+ helper?.addColorScheme(newValue);
});
if (colorMode.preference === "system") {
watchMedia();
I think the initialisation to an empty object in https://github.com/nuxt-modules/color-mode/blob/0d0e09702ade91f59fc69a82d2da780a2e6cd7db/src/runtime/plugin.client.ts#L8 is a mistake. This leads to the variable being defined, but it does not provide all the required functions.
Apparently, under some circumstances, the watchers fire when the helper
is still an empty object.
We could either guard against the helper being undefined (and also not initialise it as an empty object) or give the initial helper the required functions so calling them does not throw an error.
I am just not sure which one is the best solution, as I just landed here by drilling down on a sentry issue we saw and do not have the necessary overview of this repo to decide whats best...
Crashing with this error though, certainly is not optimal, so if I can help somehow to resolve this, I am willing to do so!
Getting this error only on certain pages... It's very inconsistent. I have no indication of what part of my code might cause it because on my machine I do not get this error.