{"record":{"id":"84342e2f52277547","repo":"parcel-bundler/parcel","slug":"import-is-not-allowed-in-asset-env-isworklet","errorCode":null,"errorMessage":"import() is not allowed in ${asset.env.isWorklet() ? 'worklets' : 'service workers'}.","messagePattern":"import\\(\\) is not allowed in (.+?)\\.","errorType":"exception","errorClass":"ThrowableDiagnostic","httpStatus":null,"severity":"error","filePath":"packages/transformers/js/src/JSTransformer.js","lineNumber":936,"sourceCode":"                  codeHighlights: [convertSourceLocationToHighlight(loc)],\n                },\n              ],\n              hints: ['Try using a static `import`.'],\n            };\n\n            if (asset.env.loc) {\n              diagnostic.codeFrames.push({\n                filePath: asset.env.loc.filePath,\n                codeHighlights: [\n                  convertSourceLocationToHighlight(\n                    asset.env.loc,\n                    'The environment was originally created here',\n                  ),\n                ],\n              });\n            }\n\n            throw new ThrowableDiagnostic({\n              diagnostic,\n            });\n          }\n\n          // If all of the target engines support dynamic import natively,\n          // we can output native ESM if scope hoisting is enabled.\n          // Only do this for scripts, rather than modules in the global\n          // output format so that assets can be shared between the bundles.\n          let outputFormat = asset.env.outputFormat;\n          if (\n            asset.env.sourceType === 'script' &&\n            asset.env.shouldScopeHoist &&\n            asset.env.supports('dynamic-import', true)\n          ) {\n            outputFormat = 'esmodule';\n          }\n\n          env = {","sourceCodeStart":918,"sourceCodeEnd":954,"githubUrl":"https://github.com/parcel-bundler/parcel/blob/59484858a1a0bcbb71f74088956bb437a2db6505/packages/transformers/js/src/JSTransformer.js#L918-L954","documentation":"Thrown as a ThrowableDiagnostic when SWC reports a DynamicImport dependency inside an asset whose env is a worklet (asset.env.isWorklet()) or whose env.context === 'service-worker'. Dynamic import() is not supported in those contexts per the HTML spec, so Parcel refuses rather than emit broken output. The diagnostic includes a code frame at the import() and, if available, the original env creation site.","triggerScenarios":"Source code in a worklet (e.g. AudioWorklet, CSS Layout/Worklet) or a service-worker module contains a dynamic `import()` expression. SWC flags the dependency as kind DynamicImport, and the JS transformer checks the env before codegen.","commonSituations":"Service-worker code that lazily imports modules; AudioWorklet processors using import(); migration from a bundler that silently polyfilled dynamic import in workers.","solutions":["Replace dynamic import() with a static `import` at the top of the worklet/service-worker file.","If conditional code loading is required, split into separate service-worker bundles and import them via importScripts() (service workers) or the worklet's addModule() API.","Move the lazily loaded logic into the main thread and message the worker instead."],"exampleFix":"// before (service-worker.js)\nself.addEventListener('install', () => {\n  import('./feature').then(m => m.register());\n});\n\n// after\nimport {register} from './feature';\nself.addEventListener('install', () => register());","handlingStrategy":"validation","validationCode":"// Lint source for dynamic import in worklet/service-worker modules\nconst DYNAMIC_IMPORT_RE = /\\bimport\\s*\\(/;\nfunction scanForDynamicImport(sources) {\n  return sources.filter(s => /(?:service-?worker|worklet)/i.test(s.path) && DYNAMIC_IMPORT_RE.test(s.code));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Static-import everything at the top of worker/worklet modules.","Add an ESLint override banning dynamic import in service-worker/worklet globs."],"tags":["service-worker","worklet","dynamic-import","swc"],"backgroundTag":null,"analyzedSha":"59484858a1a0bcbb71f74088956bb437a2db6505","analyzedAt":"2026-08-13T04:06:35.925Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}