vercel/next.js · error

Invalid escape character at the end.

Error message

Invalid escape character at the end.

What it means

Thrown by tokenizeArgs when parsing NODE_OPTIONS (or any args string) and a backslash escape appears as the very last character inside a quoted string with no character following it. An escape needs a character to escape, so a trailing backslash is invalid.

Source

Thrown at packages/next/src/server/lib/utils.ts:81

 * values and escaped characters.
 * Converted from: https://github.com/nodejs/node/blob/c29d53c5cfc63c5a876084e788d70c9e87bed880/src/node_options.cc#L1401
 *
 * @param input The arguments string to be tokenized.
 * @returns An array of strings with the tokenized arguments.
 */
export const tokenizeArgs = (input: string): string[] => {
  let args: string[] = []
  let isInString = false
  let willStartNewArg = true

  for (let i = 0; i < input.length; i++) {
    let char = input[i]

    // Skip any escaped characters in strings.
    if (char === '\\' && isInString) {
      // Ensure we don't have an escape character at the end.
      if (input.length === i + 1) {
        throw new Error('Invalid escape character at the end.')
      }

      // Skip the next character.
      char = input[++i]
    }
    // If we find a space outside of a string, we should start a new argument.
    else if (char === ' ' && !isInString) {
      willStartNewArg = true
      continue
    }

    // If we find a quote, we should toggle the string flag.
    else if (char === '"') {
      isInString = !isInString
      continue
    }

    // If we're starting a new argument, we should add it to the array.

View on GitHub (pinned to 0ae8c72462)

Solutions

  1. Inspect NODE_OPTIONS and remove or fix the trailing backslash.
  2. Ensure any backslash inside a quoted NODE_OPTIONS value escapes a real following character.
  3. Avoid backslashes in NODE_OPTIONS unless intentionally escaping a quote/space.

Example fix

# before
export NODE_OPTIONS='--max-old-space-size=4096 --title="app\'
# the trailing backslash is invalid

# after
export NODE_OPTIONS='--max-old-space-size=4096 --title="app"'
Defensive patterns

Strategy: validation

Validate before calling

function validateArgsString(input: string): void {
  if (/\\$/.test(input) || (/\\./.test(input) && input.endsWith('\\'))) {
    // crude check; better: ensure no backslash is the final char inside a quote
  }
}

Type guard

function hasTrailingEscape(input: string): boolean {
  // returns true if a backslash is the last char inside a quoted region
  let inStr = false
  for (let i = 0; i < input.length; i++) {
    if (input[i] === '\\' && inStr && i === input.length - 1) return true
    if (input[i] === '\\' && inStr) { i++; continue }
    if (input[i] === '"') inStr = !inStr
  }
  return false
}

Try / catch

try {
  tokenizeArgs(process.env.NODE_OPTIONS || '')
} catch (e) {
  if (e.message.includes('escape character')) {
    console.error('Fix NODE_OPTIONS: trailing backslash detected')
    process.exit(1)
  }
}

Prevention

When it happens

Trigger: The NODE_OPTIONS environment variable ends with a backslash inside a quoted value, e.g. NODE_OPTIONS='--title="foo\'. The tokenizer hits the backslash at i === input.length-1.

Common situations: Misconstructed NODE_OPTIONS in shell scripts, Docker ENV, or CI config where a trailing backslash escapes the closing quote or is left dangling. Shell line-continuation backslashes accidentally included.

Related errors


AI-assisted analysis of vercel/next.js@0ae8c72462 (2026-08-06). Data as JSON: /api/errors/22a48894847f29ea. Report an issue: GitHub.