N
Nuxt2d ago
Zusor

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
import { setInterval } from "/_nuxt/@fs/C:/Users/Tobias/WebstormProjects/tally/node_modules/.pnpm/nuxt@4.1.0_@parcel+watcher@2.5.1_@types+node@24.3.0_@vue+compiler-sfc@3.5.21_db0@0.3.2_ioredi_kafnzuo23ez3kp4fs4kd7hv64i/node_modules/nuxt/dist/app/compat/interval.js?v=4c3d3eb9";
importScripts("/_nuxt/@vite/env")
;
let interval = null;
console.log("Hello world");
self.addEventListener(
"message",
(e) => {
setInterval(() => {
self.postMessage("tick");
}, e.data.interval);
},
false
);
import { setInterval } from "/_nuxt/@fs/C:/Users/Tobias/WebstormProjects/tally/node_modules/.pnpm/nuxt@4.1.0_@parcel+watcher@2.5.1_@types+node@24.3.0_@vue+compiler-sfc@3.5.21_db0@0.3.2_ioredi_kafnzuo23ez3kp4fs4kd7hv64i/node_modules/nuxt/dist/app/compat/interval.js?v=4c3d3eb9";
importScripts("/_nuxt/@vite/env")
;
let interval = null;
console.log("Hello world");
self.addEventListener(
"message",
(e) => {
setInterval(() => {
self.postMessage("tick");
}, e.data.interval);
},
false
);
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
kapa.ai
kapa.ai2d ago
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: ----------------------
kapa.ai
kapa.ai2d ago
<@124906016063094785> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Zusor
ZusorOP2d ago
Okay I think this is actually just a bug I will make an issue

Did you find this page helpful?