vitest-dev/vitest · error

Root project is not initialized. This means that the Vite…

Error message

Root project is not initialized. This means that the Vite server was not established yet and the the workspace config is not resolved.

What it means

`Vitest.getRootProject()` returns the project that holds the resolved root/global config and is only meaningful once the Vite server is up and the workspace has been resolved (`coreWorkspaceProject` is populated during server bootstrap). Calling it before that handshake — e.g. inside a plugin hook that fires during initial config resolution — throws this internal-state error.

Solutions

  1. Await the full Vitest constructor (`await createVitest('test')`) before calling `getRootProject()`.
  2. Move the access into a hook that fires after server start (e.g. `onInit` / `onBeforeRun`), where the root project is guaranteed to exist.
  3. Guard with `if (vitest.coreWorkspaceProject)` before access if you must call during early phases.

Example fix

// before
const vitest = createVitest('test', config)   // forgot await
vitest.getRootProject()
// after
const vitest = await createVitest('test', config)
vitest.getRootProject()
Defensive patterns

Strategy: try-catch

Validate before calling

const vitest = await createVitest('test', config) // ensure fully started
if (!vitest.coreWorkspaceProject) {
  throw new Error('Vitest server not yet initialised; await createVitest before reading root project.')
}
const root = vitest.getRootProject()

Type guard

function isVitestReady(vitest: { coreWorkspaceProject: unknown }): boolean {
  return Boolean(vitest.coreWorkspaceProject)
}

Try / catch

try {
  return vitest.getRootProject()
} catch (err) {
  if (err instanceof Error && /Root project is not initialized/.test(err.message)) {
    // defer access to an onSetServer / onInit hook and retry
    return null
  }
  throw err
}

Prevention

When it happens

Trigger: Calling `vitest.getRootProject()` from a `configResolved` hook or before `await createVitest(...)` has resolved; reaching into the Vitest instance from a serverless handler that constructs but does not start Vitest.

Common situations: Custom reporter/plugin that eagerly inspects the root project; tests of the Vitest API that forget to await server start.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/68129101e1cc7d7a. Report an issue: GitHub.

Appendix: source

Thrown at packages/vitest/src/node/core.ts:591

  public getProvidedContext(): ProvidedContext {
    return this.getRootProject().getProvidedContext()
  }

  /** @internal */
  _ensureRootProject(): TestProject {
    if (this.coreWorkspaceProject) {
      return this.coreWorkspaceProject
    }
    this.coreWorkspaceProject = TestProject._createBasicProject(this)
    return this.coreWorkspaceProject
  }

  /**
   * Return project that has the root (or "global") config.
   */
  public getRootProject(): TestProject {
    if (!this.coreWorkspaceProject) {
      throw new Error(`Root project is not initialized. This means that the Vite server was not established yet and the the workspace config is not resolved.`)
    }
    return this.coreWorkspaceProject
  }

  public get serializedRootConfig(): SerializedRootConfig {
    return {
      ...this.getRootProject().serializedConfig,
      projects: this.projects.map(project => project.serializedConfig),
    }
  }

  public getProjectByName(name: string): TestProject {
    const project = this.projects.find(p => p.name === name)
      || this.coreWorkspaceProject
      || this.projects[0]
    if (!project) {
      throw new Error(`Project "${name}" was not found.`)
    }

View on GitHub (pinned to 1fa9837ec2)