vitejs/vite · error · Error
${this.name} environment.pluginContainer called before initi
Error message
${this.name} environment.pluginContainer called before initialized What it means
`ScanEnvironment.pluginContainer` is a lazy getter that throws until `init()` has populated `_pluginContainer`. The scan environment is a restricted view used during dependency scanning, and accessing its plugin container before initialization (scan.ts:50) indicates an ordering bug — the container isn't built until `ScanEnvironment.init()` runs.
Source
Thrown at packages/vite/src/node/optimizer/scan.ts:50
virtualModuleRE,
} from '../utils'
import type { EnvironmentPluginContainer } from '../server/pluginContainer'
import {
ERR_CLOSED_SERVER,
createEnvironmentPluginContainer,
} from '../server/pluginContainer'
import { BaseEnvironment } from '../baseEnvironment'
import type { DevEnvironment } from '../server/environment'
import { transformGlobImport } from '../plugins/importMetaGlob'
import { cleanUrl } from '../../shared/utils'
import { getRollupJsxPresets } from '../plugins/oxc'
export class ScanEnvironment extends BaseEnvironment {
mode = 'scan' as const
get pluginContainer(): EnvironmentPluginContainer {
if (!this._pluginContainer)
throw new Error(
`${this.name} environment.pluginContainer called before initialized`,
)
return this._pluginContainer
}
/**
* @internal
*/
_pluginContainer: EnvironmentPluginContainer | undefined
async init(): Promise<void> {
if (this._initiated) {
return
}
this._initiated = true
this._pluginContainer = await createEnvironmentPluginContainer(
this,
this.plugins,
undefined,View on GitHub (pinned to 89620f09af)
Solutions
- Always `await environment.init()` before reading `environment.pluginContainer`.
- If you built a custom ScanEnvironment, ensure `_pluginContainer` is assigned in `init()` exactly as the base class does.
- Reorder your setup so plugin-container access happens after the environment is initialized.
- Report as a Vite internal bug if you're not constructing the environment yourself.
Example fix
// before const env = new ScanEnvironment(...) const container = env.pluginContainer // throws // after const env = new ScanEnvironment(...) await env.init() const container = env.pluginContainer
Defensive patterns
Strategy: validation
Validate before calling
function assertScanInitialized(env: ScanEnvironment) {
if (!('_pluginContainer' in env) || (env as any)._pluginContainer == null)
throw new Error('Call await environment.init() before accessing pluginContainer')
} Type guard
function isScanReady(env: ScanEnvironment): boolean {
return (env as any)._pluginContainer != null
} Prevention
- Always await environment.init() before touching pluginContainer on a ScanEnvironment.
- Treat pluginContainer as lazy/async-initialized; never read it in constructors.
When it happens
Trigger: Calling `environment.pluginContainer` on a `ScanEnvironment` (or a `devToScanEnvironment` wrapper) before awaiting its `init()`. Typically an internal Vite path or a custom Environment API consumer touching the container too early.
Common situations: Custom environments using the experimental Environment API that access `pluginContainer` in a constructor/before-init hook; framework integrations racing plugin setup against scan init; Vite internal regression around scan ordering.
Related errors
- Vite Internal Error: registerMissingImport is not supported
- Can not commit a Deps Optimization run as it was cancelled
- The build was canceled
- The following dependencies are imported but could not be res
- Unable to parse: ${filePath}.
AI-assisted analysis of vitejs/vite@89620f09af (2026-08-03).
Data as JSON: /data/errors/5ee2c85aa9f478f2.json.
Report an issue: GitHub.