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
- Move '=>' onto the same line as the closing parenthesis / parameter list.
- If line-wrapping is desired, wrap the parameters but keep '=>' adjacent to ')'.
- 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
- Run tsc/eslint before bundling; both flag malformed arrow functions.
- Configure prettier with arrow-body rules that keep '=>' on the parameter line.
- Review any refactor that line-wraps an arrow function.
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
- Invalid binding pattern
- Unexpected ":"
- Unexpected "..."
- Unexpected newline after "async"
- Unexpected newline after "type"
AI-assisted analysis of evanw/esbuild@f6058f8364 (2026-08-09).
Data as JSON: /api/errors/3266f779a0349c03.
Report an issue: GitHub.