SyntaxError: Unexpected token '?' (Node.js) — how to fix
/app/node_modules/some-lib/index.js:42
const timeout = options.timeout ?? 5000;
^
SyntaxError: Unexpected token '?'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
Quick fix: Check node -v, then upgrade to a current LTS: nvm install --lts && nvm use --lts (or install from nodejs.org / NodeSource). Make sure the shell, IDE, cron job, CI image and Docker base image all use the new Node (which node). If you must stay on old Node, pin the dependency to an older version.
What it means
?? (nullish coalescing) and ?. (optional chaining) are ES2020 syntax. Node.js supports them from version 14. On Node 12 and older the parser doesn't know these tokens and fails before any code runs. Depending on where it trips you see:
SyntaxError: Unexpected token '?'fora ?? bSyntaxError: Unexpected token '.'fora?.b- a similar
SyntaxErrorfora ??= bon Node 14 (logical assignment needs Node 15+)
The path in the first line tells you whether the syntax is in your code or in node_modules. In node_modules it means a dependency dropped support for your Node version. The stack frames internal/modules/cjs/loader.js are typical of old Node.
Fix 1: upgrade Node
node -v
With nvm:
nvm install --lts
nvm use --lts
nvm alias default 'lts/*'
On Windows use nvm-windows or the installer from nodejs.org. On Debian/Ubuntu the distro nodejs package is often old; use NodeSource or nvm instead of apt install nodejs.
After upgrading, reinstall dependencies so native modules are rebuilt:
rm -rf node_modules
npm ci # or npm install
Fix 2: the wrong Node is still being used
You upgraded but the error remains. Something runs another binary:
which -a node
node -v
Common culprits:
- CI: set the version explicitly, for example
actions/setup-nodewithnode-version: 20. - Docker: the base image pins Node. Change
FROM node:12to a current tag such asFROM node:20-alpine. - IDE / systemd / cron: they don't load your shell's nvm setup and fall back to
/usr/bin/node. Use an absolute path to the new binary. - Elastic Beanstalk, Heroku, Vercel etc.: set the platform's Node version (often
"engines": { "node": ">=18" }inpackage.json).
Add an .nvmrc so everyone uses the same version:
node -v > .nvmrc
Fix 3: stay on old Node (temporary)
If you can't upgrade yet, install the last version of the dependency that still supported your Node. Check its changelog or engines field:
npm view some-lib engines
npm install some-lib@<older-version>
npm install shows EBADENGINE warnings for packages that declare a newer Node, which helps you find the culprit.
Fix 4: browser bundles and old build tools
If the error comes from a bundler (for example webpack 4 reporting Module parse failed: Unexpected token on ?.), the bundler's parser is too old. Upgrade the bundler, or transpile that dependency with Babel. For your own code shipped to old browsers, set a Babel/TypeScript target that rewrites ?. and ?? (TypeScript does this for any target below ES2020).
Last updated 2026-11-29.