SyntaxError: Unexpected token '?' running cdk synth on Amazon Linux 2 CodeBuild (Node 14 required since...

Running cdk synth on an Amazon Linux 2 CodeBuild image started failing with SyntaxError: Unexpected token '?', pointing at the compiled aws-cdk CLI JavaScript. Nothing in the project had changed; the install step just pulled the newest aws-cdk. The cause was that aws-cdk 2.28.0 raised its Node requirement to >= 14.15.0 (it uses the ?? operator), while the Amazon Linux 2 build images only ship Node 12, and the ARM images had no newer option at all.

aws-cdk 2.28.0 and later require Node.js 14.15.0 or newer, which the Amazon Linux 2 CodeBuild images do not provide. The verified workaround is to install a newer Node in the build's install phase with the n version manager, e.g. add an install command like n 16.15.1 before running cdk commands. The reporter confirmed this unblocked their pipeline. Longer term, move to a CodeBuild image with a newer Node runtime. Source: https://github.com/aws/aws-cdk/issues/20739