{"record":{"id":"a60d1df8b8ace542","repo":"usebruno/bruno","slug":"creating-a-env-variable-without-specifying-a-name","errorCode":null,"errorMessage":"Creating a env variable without specifying a name is not allowed.","messagePattern":"Creating a env variable without specifying a name is not allowed\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/bruno-js/src/bru.js","lineNumber":200,"sourceCode":"  getEnvName() {\n    return this.envVariables.__name__;\n  }\n\n  getProcessEnv(key) {\n    return this.processEnvVars[key];\n  }\n\n  hasEnvVar(key) {\n    return Object.hasOwn(this.envVariables, key);\n  }\n\n  getEnvVar(key) {\n    return this.interpolate(this.envVariables[key]);\n  }\n\n  setEnvVar(key, value) {\n    if (!key) {\n      throw new Error('Creating a env variable without specifying a name is not allowed.');\n    }\n\n    if (variableNameRegex.test(key) === false) {\n      throw new Error(\n        `Variable name: \"${key}\" contains invalid characters! Names must only contain alpha-numeric characters, \"-\", \"_\", \".\"`\n      );\n    }\n\n    // Deep-equal compare so object/array writes that mutate in place\n    // (e.g. `const c = bru.getEnvVar('cfg'); c.port = 4000; bru.setEnvVar('cfg', c);`)\n    // still flip the dirty flag — strict `!==` returned false for same-reference writes.\n    if (!Object.hasOwn(this.envVariables, key) || !isEqual(this.envVariables[key], value)) {\n      this.envVariables[key] = value;\n      this._envDirty = true;\n    }\n  }\n\n  deleteEnvVar(key) {","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/usebruno/bruno/blob/9bdd81c7bdc57006e5f5ebffb79321a8d979f712/packages/bruno-js/src/bru.js#L182-L218","documentation":"Thrown by bru.setEnvVar(key, value) when the key argument is falsy (undefined, null, empty string, 0, false). The guard at bru.js:199 checks !key and rejects any falsy value before proceeding to validate the key format and set the variable in the environment scope.","triggerScenarios":"Calling bru.setEnvVar(undefined, 'value'), bru.setEnvVar('', 'value'), bru.setEnvVar(null, 'value'), or passing a variable name computed dynamically that evaluates to empty (e.g., bru.setEnvVar(req.getUrl(), 'value') when the URL is empty).","commonSituations":"Using a dynamically computed key from request data that may be empty (e.g., extracting a header that is not present). Forgetting to pass the key argument. Using a variable that has not been initialized as the key.","solutions":["Provide a non-empty, static string literal as the key: bru.setEnvVar('token', value).","Validate the key is a non-empty string before calling setEnvVar.","Use a fallback if the key source may be empty: bru.setEnvVar(key || 'default', value)."],"exampleFix":"// before\nconst key = res.getBody()?.name; // may be undefined\nbru.setEnvVar(key, 'active');\n\n// after\nconst key = res.getBody()?.name;\nif (key) {\n  bru.setEnvVar(key, 'active');\n}","handlingStrategy":"validation","validationCode":"function isValidEnvVarKey(key) {\n  return typeof key === 'string' && key.length > 0;\n}\n// before calling: if (isValidEnvVarKey(key)) bru.setEnvVar(key, value);","typeGuard":null,"tryCatchPattern":"try {\n  bru.setEnvVar(key, value);\n} catch (e) {\n  if (e.message.includes('without specifying a name')) {\n    console.error('Environment variable key was empty or missing');\n  }\n}","preventionTips":["Always pass a non-empty string literal as the key argument.","Guard dynamic keys with a truthiness check.","Use a fallback key when the source may be empty."],"tags":["env-var","validation","setenvvar","empty-key"],"backgroundTag":null,"analyzedSha":"9bdd81c7bdc57006e5f5ebffb79321a8d979f712","analyzedAt":"2026-08-13T04:09:25.751Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}