mastra-ai/mastra · error · Error
Invalid ENV value for ${key}: multiline values are not suppo
Error message
Invalid ENV value for ${key}: multiline values are not supported. What it means
FileEnvService.validateEnvEntry throws this when the VALUE for an env entry contains a carriage return or newline. .env files are line-based (KEY=VALUE per line), so multiline values would corrupt parsing of subsequent entries. The CLI rejects such values up front rather than writing a broken file.
Source
Thrown at packages/cli/src/services/service.env.ts:22
abstract getEnvValue(key: string): Promise<string | null>;
abstract setEnvValue(key: string, value: string): Promise<void>;
}
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
export class FileEnvService extends EnvService {
private static readonly ENV_KEY_REGEX = /^[A-Za-z_][A-Za-z0-9_]*$/;
private readonly filePath: string;
private validateEnvEntry(key: string, value: string): void {
if (!FileEnvService.ENV_KEY_REGEX.test(key)) {
throw new Error(`Invalid ENV key: ${key}`);
}
if (/[\r\n]/.test(value)) {
throw new Error(`Invalid ENV value for ${key}: multiline values are not supported.`);
}
}
constructor(filePath: string) {
super();
this.filePath = filePath;
}
private envLineRegex(key: string, captureValue = false): RegExp {
const pattern = captureValue ? `^${escapeRegExp(key)}=(.*)$` : `^${escapeRegExp(key)}=.*$`;
return new RegExp(pattern, 'm');
}
private async updateEnvData({
key,
value,
filePath = this.filePath,
data,View on GitHub (pinned to 75dd419e61)
Solutions
- Replace newlines with an escape sequence, e.g. '\\n', and decode on read
- Base64-encode multiline content: Buffer.from(value).toString('base64')
- Strip whitespace: value.replace(/[\r\n]+/g, ' ') or .trim() if it is only trailing
- Store multiline secrets in a file and pass a path in the env var instead
Example fix
// before
await service.updateEnvData({ PRIVATE_KEY: fs.readFileSync('key.pem', 'utf8') });
// after
await service.updateEnvData({ PRIVATE_KEY: Buffer.from(fs.readFileSync('key.pem', 'utf8')).toString('base64') }); Defensive patterns
Strategy: validation
Validate before calling
function assertSingleLineEnvValue(key: string, value: string) {
if (/\r|\n/.test(value)) throw new Error(`Env value for ${key} must not contain newlines`);
}
// or encode multiline content:
const encoded = /\r|\n/.test(value) ? Buffer.from(value).toString('base64') : value; Type guard
const isSingleLine = (value: string): boolean => !/[\r\n]/.test(value);
Try / catch
try {
await service.updateEnvData(entries);
} catch (e) {
if (e instanceof Error && e.message.startsWith('Invalid ENV value')) {
console.error('Multiline env values are unsupported; base64-encode or flatten them.');
} else throw e;
} Prevention
- Trim values read from files/terminals to drop stray trailing newlines
- Encode multiline content (base64 or \\n escapes) before storing in .env
- Keep multiline secrets in dedicated files referenced by path
- Validate all entries with a single pre-write check loop
When it happens
Trigger: Calling updateEnvData with a value containing '\n' or '\r', e.g. multiline secrets (JSON blobs, private keys, certificates) pasted as-is, or values read from a multiline source.
Common situations: Storing a private key or JSON as an env var without encoding; copying a value from a terminal that included a trailing newline; generating values programmatically from multiline input.
Related errors
- Invalid ENV key: ${key}
- Invalid environment variable name: "${key}"
- ClaudeSDKAgent resumeData must include sessionId or continue
- CursorSDKAgent resumeData must include a message.
- CursorSDKAgent resumeData.agentId must be a string when prov
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/f56ea9433507c6dc.
Report an issue: GitHub.