NextJS build fails due to nullish coalescing operator

I’m trying to deploy NextJS application with Chakra UI on Cloudflare pages. The build kept failing with the message: SyntaxError: Unexpected token ?

I set the tsconfig target to ES2015, even though it seems to be failing to build.

The exact error message is this↓

|18:49:18.621|info  - Linting and checking validity of types...|
|---|---|
|18:49:22.730|info  - Creating an optimized production build...|
|18:49:31.798|info  - Compiled successfully|
|18:49:31.798|info  - Collecting page data...|
|18:49:32.268|/opt/buildhome/repo/node_modules/@chakra-ui/accordion/dist/index.cjs.js:136|
|18:49:32.269|        return defaultIndex ?? [];|
|18:49:32.270|                             ^|
|18:49:32.270||
|18:49:32.270|SyntaxError: Unexpected token '?'|
|18:49:32.270|    at wrapSafe (internal/modules/cjs/loader.js:1054:16)|
|18:49:32.270|    at Module._compile (internal/modules/cjs/loader.js:1102:27)|
|18:49:32.271|    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)|
|18:49:32.271|    at Module.load (internal/modules/cjs/loader.js:986:32)|
|18:49:32.271|    at Function.Module._load (internal/modules/cjs/loader.js:879:14)|
|18:49:32.271|    at Module.require (internal/modules/cjs/loader.js:1026:19)|
|18:49:32.271|    at require (internal/modules/cjs/helpers.js:72:18)|
|18:49:32.271|    at Object.<anonymous> (/opt/buildhome/repo/node_modules/@chakra-ui/react/dist/index.cjs.js:36:25)|
|18:49:32.271|    at Module._compile (internal/modules/cjs/loader.js:1138:30)|
|18:49:32.271|    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)|
|18:49:32.271||
|18:49:32.272|> Build error occurred|
|18:49:32.273|Error: Failed to collect page data for /|
|18:49:32.273|    at /opt/buildhome/repo/node_modules/next/dist/build/utils.js:916:15|
|18:49:32.273|    at processTicksAndRejections (internal/process/task_queues.js:97:5) {|
|18:49:32.273|  type: 'Error'|
|18:49:32.274|}|
|18:49:32.301|npm ERR! code ELIFECYCLE|
|18:49:32.303|npm ERR! errno 1|
|18:49:32.303|npm ERR! [email protected] build: `next build`|
|18:49:32.303|npm ERR! Exit status 1|
|18:49:32.304|npm ERR! |
|18:49:32.304|npm ERR! Failed at the [email protected] build script.|
|18:49:32.304|npm ERR! This is probably not a problem with npm. There is likely additional logging output above.|
|18:49:32.308||
|18:49:32.309|npm ERR! A complete log of this run can be found in:|
|18:49:32.310|npm ERR!     /opt/buildhome/.npm/_logs/2022-12-11T13_19_32_297Z-debug.log|
|18:49:32.317|Failed: build command exited with code: 1|

Can anybody tell me how to fix this build failure?

Try setting your NODE_VERSION environment variable to a more modern version of Node.js such as 16.

Pages uses an older version (12) by default which doesn’t support newer syntax such as nullish coalescing.

2 Likes

That was a quick response. It worked out. Thank you so much for the help :slight_smile:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.