{"record":{"id":"b92dcb92b751036e","repo":"yarnpkg/yarn","slug":"failed-to-replace-env-in-config-match","errorCode":null,"errorMessage":"Failed to replace env in config: ${match}","messagePattern":"Failed to replace env in config: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/util/env-replace.js","lineNumber":16,"sourceCode":"/* @flow */\nconst ENV_EXPR = /(\\\\*)\\$\\{([^}]+)\\}/g;\n\nexport type Env = {[key: string]: ?string};\n\nexport default function envReplace(value: string, env: Env = process.env): string {\n  if (typeof value !== 'string' || !value) {\n    return value;\n  }\n\n  return value.replace(ENV_EXPR, (match: string, esc: string, envVarName: string) => {\n    if (esc.length && esc.length % 2) {\n      return match;\n    }\n    if (undefined === env[envVarName]) {\n      throw new Error('Failed to replace env in config: ' + match);\n    }\n    return env[envVarName] || '';\n  });\n}\n","sourceCodeStart":1,"sourceCodeEnd":21,"githubUrl":"https://github.com/yarnpkg/yarn/blob/c2dda503f3759b5be5f0e24ecd9cf5c97a540147/src/util/env-replace.js#L1-L21","documentation":"envReplace() substitutes ${VAR} and $VAR patterns in config values. If a referenced environment variable is undefined, it throws rather than silently leaving a broken value. This guards configs from silently deploying with unexpanded placeholders.","triggerScenarios":"A config value (e.g., in .npmrc) references an env var via ${VAR} or $VAR that is not set in the current environment. ENV_EXPR matches but env[envVarName] is undefined.","commonSituations":"CI without the expected env var; configs shared between machines with different env setups; typo in the variable name; escaped backslashes producing an unexpected match.","solutions":["Set the referenced environment variable in your shell or CI configuration","Remove or correct the ${VAR} reference in the config file","Provide a default value in a wrapper script before running Yarn"],"exampleFix":"# before (.npmrc)\n//registry.npmjs.org/:_authToken=${NPM_TOKEN}\n# after — export the var first\nexport NPM_TOKEN=xxxxx","handlingStrategy":"validation","validationCode":"function envVarsReferenced(value: string): string[] {\n  const matches = value.match(/\\$\\{(\\w+)\\}|\\$(\\w+)/g) || [];\n  return matches.map(m => m.replace(/[${}]/g, ''));\n}\nconst missing = envVarsReferenced(configValue).filter(v => !(v in process.env));\nif (missing.length) {\n  throw new Error(`Unset env vars referenced in config: ${missing.join(', ')}`);\n}","typeGuard":"function allEnvVarsDefined(value: string, env: object): boolean {\n  const re = /\\$\\{(\\w+)\\}|\\$(\\w+)/g;\n  let m;\n  while ((m = re.exec(value))) {\n    const name = m[1] || m[2];\n    if (!(name in env)) return false;\n  }\n  return true;\n}","tryCatchPattern":"try {\n  const replaced = envReplace(value);\n} catch (e) {\n  if (e.message.includes('Failed to replace env')) {\n    // substitute a default or prompt the user to set the var\n  }\n}","preventionTips":["Set all referenced env vars in CI before running Yarn","Document required env vars for shared config files","Use a .env loader or pre-flight check script"],"tags":["env","config","env-replace"],"backgroundTag":null,"analyzedSha":"c2dda503f3759b5be5f0e24ecd9cf5c97a540147","analyzedAt":"2026-08-13T04:17:06.305Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}