Prevent Nuxt from injecting setInterval import into worker
I have a worker I'm importing using
?worker
which is using setInterval
.
Nuxt "helpfully" adds this handler for SSR as an import
As regular import Syntax doesn't work in Workers, this leads to an error. How do I tell Nuxt not to do that?3 Replies
Hi @Zusor. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
It’s Nuxt’s polyfill injection for timers. There’s a recent change that adds an option to disable auto‑imported polyfills like setInterval specifically for cases like workers/tests (feat: allow disabling auto‑imported polyfills). However, the docs in the provided sources don’t show the exact config key or its availability in a released version, so I can’t confirm the precise nuxt.config usage.
Workarounds based on the sources:
- Avoid Nuxt’s injected imports by running the worker code in a context where Nuxt doesn’t transform it, or by importing the native API directly (the motivation for the polyfill option is to avoid overriding native APIs like setInterval) (feat: allow disabling auto‑imported polyfills).
- If you’ve globally disabled auto‑imports, note that explicit imports from #imports are still possible in app code, but workers need native globals and don’t support ESM import in that form; disabling such injections is exactly what the new polyfills option targets (Disabling auto-imports).
If you’re on a Nuxt version that includes the polyfills option from the PR, use that to disable timer polyfills for your build. If not, you may need to pin Nuxt to a version without this injection, or externalize the worker script so it isn’t processed by Nuxt/Vite transforms until the option is available.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@124906016063094785> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Okay I think this is actually just a bug I will make an issue