yarnpkg/yarn · error · TypeError

Invalid number of spaces

Error message

Invalid number of spaces

What it means

Thrown as a `TypeError` by the lockfile tokenizer when a line begins with an odd number of leading spaces (`indentSize % 2 !== 0`). The yarn.lock format uses two-space indentation per level, so any odd indent count is syntactically invalid and the tokenizer refuses to guess the nesting level.

Source

Thrown at src/lockfile/parse.js:89

    } else if (input[0] === '#') {
      chop++;

      let nextNewline = input.indexOf('\n', chop);
      if (nextNewline === -1) {
        nextNewline = input.length;
      }
      const val = input.substring(chop, nextNewline);
      chop = nextNewline;
      yield buildToken(TOKEN_TYPES.comment, val);
    } else if (input[0] === ' ') {
      if (lastNewline) {
        let indentSize = 1;
        for (let i = 1; input[i] === ' '; i++) {
          indentSize++;
        }

        if (indentSize % 2) {
          throw new TypeError('Invalid number of spaces');
        } else {
          chop = indentSize;
          yield buildToken(TOKEN_TYPES.indent, indentSize / 2);
        }
      } else {
        chop++;
      }
    } else if (input[0] === '"') {
      let i = 1;
      for (; i < input.length; i++) {
        if (input[i] === '"') {
          const isEscaped = input[i - 1] === '\\' && input[i - 2] !== '\\';
          if (!isEscaped) {
            i++;
            break;
          }
        }
      }

View on GitHub (pinned to c2dda503f3)

Solutions

  1. Open yarn.lock and re-indent the offending line(s) to a multiple of two spaces.
  2. Regenerate the lockfile from scratch: `rm yarn.lock && yarn install`.
  3. Ensure your editor uses spaces (not tabs) for yarn.lock, or expand tabs consistently.
  4. Resolve merge conflicts in yarn.lock cleanly.

Example fix

# before (yarn.lock)
react@^16.0.0:
   version "16.8.0"  # 3-space indent
# after
react@^16.0.0:
  version "16.8.0"  # 2-space indent
Defensive patterns

Strategy: validation

Validate before calling

const fs = require('fs');
function validateLockfileIndent(filePath) {
  const lines = fs.readFileSync(filePath, 'utf8').split('\n');
  lines.forEach((line, idx) => {
    const indent = line.match(/^( +)/);
    if (indent && indent[1].length % 2 !== 0) {
      console.error(`Odd indent (${indent[1].length} spaces) at line ${idx + 1}`);
    }
  });
}
validateLockfileIndent('yarn.lock');

Try / catch

try {
  const result = parseLockfile(lockfilePath);
} catch (err) {
  if (err instanceof TypeError && err.message === 'Invalid number of spaces') {
    reporter.error('yarn.lock has odd indentation — regenerate with `rm yarn.lock && yarn install`.');
    process.exit(1);
  }
  throw err;
}

Prevention

When it happens

Trigger: A yarn.lock line is indented with 1, 3, 5, etc. spaces instead of an even number. Caused by manual editing, a bad merge, or an editor that mixed tabs and spaces (tab expansion to a non-multiple-of-2 width).

Common situations: Developer hand-edits yarn.lock and mis-indents. A merge tool conflits and leaves 3-space indent. An automated tool rewrites the lockfile with inconsistent indentation.

Related errors


AI-assisted analysis of yarnpkg/yarn@c2dda503f3 (2026-08-13). Data as JSON: /api/errors/55d5f544ce946a87. Report an issue: GitHub.