evanw/esbuild · error · Error

Expected but got

Error message

Expected ${JSON.stringify(packageJSON.version)} but got ${JSON.stringify(stdout)}

What it means

At the end of postinstall, validateBinaryVersion() (lib/npm/node-install.ts:57) compares the output of `<binary> --version` against packageJSON.version and throws if they differ. This guards against a native binary whose version does not match the JavaScript package, since the two speak a versioned wire protocol and a mismatch would cause confusing runtime failures.

Solutions

  1. Remove node_modules and reinstall cleanly so the JS and native packages are the same version.
  2. Unset the ESBUILD_BINARY_PATH environment variable (or update it to a matching binary).
  3. Align esbuild versions across the whole monorepo (dedupe).
  4. Clear the package manager cache and reinstall.

Example fix

# before
export ESBUILD_BINARY_PATH=/old/path/esbuild   # stale binary

# after
unset ESBUILD_BINARY_PATH
rm -rf node_modules && npm install
Defensive patterns

Strategy: validation

Validate before calling

// Verify the on-disk binary version matches the JS package before first use.
const { execFileSync } = require('child_process')
const { version } = require('esbuild/package.json')
function assertBinaryVersion(binPath) {
  const out = execFileSync(binPath, ['--version']).toString().trim()
  if (out !== version) {
    throw new Error(`esbuild binary ${out} != js ${version}; reinstall esbuild`)
  }
}
if (process.env.ESBUILD_BINARY_PATH) assertBinaryVersion(process.env.ESBUILD_BINARY_PATH)

Prevention

When it happens

Trigger: The esbuild binary that ends up on disk reports a different --version string than the esbuild JS package's version, e.g. due to a stale cached binary, a manual ESBUILD_BINARY_PATH override, or a partially-upgraded monorepo where the JS and native halves drifted.

Common situations: Upgrading esbuild but a leftover binary from the previous version remains; ESBUILD_BINARY_PATH env var pointing at an old build; monorepo with multiple esbuild versions cross-resolving; package manager hardlinking a mismatched @esbuild/* platform package.

Related errors


AI-assisted analysis of evanw/esbuild@f6058f8364 (2026-08-09). Data as JSON: /api/errors/712951dd1d844f9b. Report an issue: GitHub.

Appendix: source

Thrown at lib/npm/node-install.ts:58

      let os = 'this version of macOS'
      try {
        os = 'macOS ' + child_process.execFileSync('sw_vers', ['-productVersion']).toString().trim()
      } catch {
      }
      throw new Error(`The "esbuild" package cannot be installed because ${os} is too outdated.

The Go compiler (which esbuild relies on) no longer supports ${os},
which means the "esbuild" binary executable can't be run. You can either:

  * Update your version of macOS to one that the Go compiler supports
  * Use the "esbuild-wasm" package instead of the "esbuild" package
  * Build esbuild yourself using an older version of the Go compiler
`)
    }
    throw err
  }
  if (stdout !== packageJSON.version) {
    throw new Error(`Expected ${JSON.stringify(packageJSON.version)} but got ${JSON.stringify(stdout)}`)
  }
}

function isYarn(): boolean {
  const { npm_config_user_agent } = process.env
  if (npm_config_user_agent) {
    return /\byarn\//.test(npm_config_user_agent)
  }
  return false
}

function fetch(url: string): Promise<Buffer> {
  return new Promise((resolve, reject) => {
    https.get(url, res => {
      if ((res.statusCode === 301 || res.statusCode === 302) && res.headers.location)
        return fetch(res.headers.location).then(resolve, reject)
      if (res.statusCode !== 200)
        return reject(new Error(`Server responded with ${res.statusCode}`))

View on GitHub (pinned to f6058f8364)