evanw/esbuild · error · LexerPanic

Unexpected newline before "=>"

Error message

Unexpected newline before "=>"

What it means

In parseArrowBody, esbuild enforces the spec's NoLineHere rule for arrow functions: a line terminator is not permitted between the parameter list and the '=>'. If HasNewlineBefore is true when the '=>' is expected (js_parser.go:2933), parsing fails.

Source

Thrown at internal/js_parser/js_parser.go:2935

			Target: target,
			Index:  js_ast.Expr{Loc: nameLoc, Data: &js_ast.ENameOfSymbol{Ref: p.symbolForMangledProp(name)}},
		}
	}

	return &js_ast.EDot{
		Target:  target,
		Name:    name,
		NameLoc: nameLoc,
	}
}

func (p *parser) parseArrowBody(args []js_ast.Arg, data fnOrArrowDataParse) *js_ast.EArrow {
	arrowLoc := p.lexer.Loc()

	// Newlines are not allowed before "=>"
	if p.lexer.HasNewlineBefore {
		p.log.AddError(&p.tracker, p.lexer.Range(), "Unexpected newline before \"=>\"")
		panic(js_lexer.LexerPanic{})
	}

	p.lexer.Expect(js_lexer.TEqualsGreaterThan)

	for _, arg := range args {
		p.declareBinding(ast.SymbolHoisted, arg.Binding, parseStmtOpts{})
	}

	// The ability to use "this" and "super" is inherited by arrow functions
	data.isThisDisallowed = p.fnOrArrowDataParse.isThisDisallowed
	data.allowSuperCall = p.fnOrArrowDataParse.allowSuperCall
	data.allowSuperProperty = p.fnOrArrowDataParse.allowSuperProperty

	if p.lexer.Token == js_lexer.TOpenBrace {
		body := p.parseFnBody(data)
		p.afterArrowBodyLoc = p.lexer.Loc()
		return &js_ast.EArrow{Args: args, Body: body}
	}

View on GitHub (pinned to f6058f8364)

Solutions

  1. Move '=>' onto the same line as the closing parenthesis / parameter list.
  2. If line-wrapping is desired, wrap the parameters but keep '=>' adjacent to ')'.
  3. Reformat with a correct arrow-function style.

Example fix

// before
const f = (a, b)
  => a + b
// after
const f = (a, b) => a + b
Defensive patterns

Strategy: validation

Validate before calling

import "github.com/evanw/esbuild/pkg/api"
res := api.Transform(src, api.TransformOptions{Loader: api.LoaderTS})
for _, m := range res.Errors {
  if strings.Contains(m.Text, "Unexpected newline before") {
    // point the editor at m.Location and reject
  }
}

Prevention

When it happens

Trigger: An arrow function where the '=>' is on a separate line from the parameter list, e.g. 'const f = (a)\n => a'.

Common situations: Hand-editing that wrapped the '=>' onto its own line; a misconfigured formatter; refactor that moved tokens across lines.

Related errors


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