avajs/ava · error · Error

Chalk has already been configured

Error message

Chalk has already been configured

What it means

Chalk's module-level `set()` function applies global options (like color level) by re-creating the chalk instance, but this can only happen once per process. If `set()` is called a second time, the library throws to prevent silently overwriting an already-configured chalk instance shared by all consumers.

Source

Thrown at lib/chalk.js:10

import {Chalk} from 'chalk'; // eslint-disable-line unicorn/import-style

let chalk = new Chalk(); // eslint-disable-line import-x/no-mutable-exports

export {chalk};

let configured = false;
export function set(options) {
	if (configured) {
		throw new Error('Chalk has already been configured');
	}

	configured = true;
	chalk = new Chalk(options);
}

View on GitHub (pinned to bbfd946322)

Solutions

  1. Remove or guard the duplicate set() call — configure chalk exactly once at process start.
  2. Track configuration with your own flag and skip the second call if already configured.
  3. If options must change, construct a new Chalk instance (`new Chalk(options)`) instead of calling set().
  4. Isolate configurations by moving one consumer into a separate process/worker.

Example fix

// before
chalk.set({level: 2});
initLogging(); // also calls chalk.set → throws
// after
if (!chalk.level) chalk.set({level: 2});
initLogging();
Defensive patterns

Strategy: validation

Validate before calling

if (chalk.level === undefined) chalk.set({level: 2});

Type guard

const isChalkConfigured = () => chalk.level !== undefined;

Try / catch

try {
  chalk.set(options);
} catch (err) {
  if (!/already been configured/.test(err.message)) throw err;
  // chalk already set up; proceed with existing configuration
}

Prevention

When it happens

Trigger: Calling `chalk.set(options)` (or an API that wraps it, e.g. chalk.level configuration via set) more than once in the same process — e.g. once in app bootstrap and again in a library, a worker, or on a config reload.

Common situations: Application code and a dependency both configure chalk; hot-reloading a module that calls set() at import time; multiple entry points (CLI + tests) each calling set(); accidentally importing the configure side-effect twice via bundlers.

Related errors


AI-assisted analysis of avajs/ava@bbfd946322 (2026-09-02). Data as JSON: /api/errors/62521f8e9c772161. Report an issue: GitHub.