Using Nullish coalescing operator (?) causes syntax error

[ note: ?? in my title keeps getting removed]

twam-gallant-swirles-d68516

I set node version to be 14.6.0, same as my dev env. and verrify it’s installed in deploy log.

Despite ?? being introduced in node 14.0.0 my function using it gets a runtime error. I have to change the code to not to use it.

Are you doing some strange syntax checking that is not using the correct version of javascript?

with this code row.map((c) => c._rawData.formattedValue ?? '') I get an error about ? causing a syntax error.

I can’t get to the exact error as following the functions on any old deploys just seems to show the latest logs :frowning:

So I changed my code back to old style

row.map((c) => c._rawData.formattedValue ? c._rawData.formattedValue : '', )

That’s not a big issue but I feel I should be able ot use the language supported by the Node version in use.

Hey @slim :wave:t2:

You may want to check out the docs on this one; I believe Netlify grants you full control over Node version as noted here:

Otherwise, the default build image (Xenial) uses Node v10 as noted here:

Hope that helps!!


Jon

Thank John but as I mentioned node is correctly set to a version that supports ?? (according to the deploy log) I’ve no idea where the syntax error is coming from.

Gotcha. As a rubber-duck debugging exercise, can you walk me through where/how you’re setting the Node version and/or share some of the deploy log here?


Jon

Sure I added some test code at the top of my function

const n = null
const v = n ?? 123
console.log(v)

local node --version = 14.6.0
.nvmrc = 14.6.0
deploy log snippet

6:03:46 PM: Attempting node version '14.6.0' from .nvmrc
6:03:46 PM: Downloading and installing node v14.6.0...
6:03:47 PM: Downloading https://nodejs.org/dist/v14.6.0/node-v14.6.0-linux-x64.tar.xz...
6:03:47 PM: Computing checksum with sha256sum
6:03:47 PM: Checksums matched!
6:03:51 PM: Now using node v14.6.0 (npm v6.14.6)

Local output when running function under netlify dev:

Request from ::ffff:127.0.0.1: GET /.netlify/functions/read-sheet2
123
Response with status 200 in 3123 ms.

as expected (n is null)

From deployed instance in Browser F12 network tab

Status502 Bad Gateway

in Function log

6:08:15 PM: 2020-08-11T17:08:15.327Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Unexpected token '?'","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '?'","    at _loadUserApp (/var/runtime/UserFunction.js:98:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1138:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)","    at Module.load (internal/modules/cjs/loader.js:986:32)","    at Function.Module._load (internal/modules/cjs/loader.js:879:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)","    at internal/main/run_main_module.js:17:47"]}
6:08:15 PM: 2020-08-11T17:08:15.545Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Unexpected token '?'","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '?'","    at _loadUserApp (/var/runtime/UserFunction.js:98:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1138:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)","    at Module.load (internal/modules/cjs/loader.js:986:32)","    at Function.Module._load (internal/modules/cjs/loader.js:879:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)","    at internal/main/run_main_module.js:17:47"]}
6:08:15 PM: Duration: 205.75 ms	Memory Usage: 15 MB	
6:08:15 PM: Unknown application error occurred
Runtime.UserCodeSyntaxError
6:09:02 PM: 2020-08-11T17:09:02.451Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Unexpected token '?'","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '?'","    at _loadUserApp (/var/runtime/UserFunction.js:98:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1138:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)","    at Module.load (internal/modules/cjs/loader.js:986:32)","    at Function.Module._load (internal/modules/cjs/loader.js:879:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)","    at internal/main/run_main_module.js:17:47"]}
6:09:02 PM: Duration: 191.04 ms	Memory Usage: 15 MB	
6:09:02 PM: Unknown application error occurred
Runtime.UserCodeSyntaxError
6:09:02 PM: 2020-08-11T17:09:02.626Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Unexpected token '?'","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '?'","    at _loadUserApp (/var/runtime/UserFunction.js:98:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1138:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)","    at Module.load (internal/modules/cjs/loader.js:986:32)","    at Function.Module._load (internal/modules/cjs/loader.js:879:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)","    at internal/main/run_main_module.js:17:47"]}

Strange it happens multiple times! As code is at head of function.

replacing the code as shown above and all is good

Hope that helps

Ah. Yeah I think I see the issue :confused:

So the NODE_VERSION I was referring to is the version used on the site build bot / container. That’s a fully separate service and concept from the Serverless Functions runtime (which is just Netlify’s [brilliant] layer around AWS Lambda). So per the docs for env var configurations - you can use a special env var, AWS_LAMBDA_JS_RUNTIME to override the Node version used in Lambda, however, following the docs to runtime settings then further to AWS’s own docs on available node versions it appears that AWS only supports Node 10 and 12. :confused:

I hope that helps. News to me too (didn’t even realize the ?? op was in 14! That’s awesome!) but I’m sure support will come soon.


Jon

1 Like

Aha - another moving part!! :stuck_out_tongue:
And I sort of knew it was lambda under the bonnet
Yes, anything that makes AWS less horrid is Brilliant and Netlify is sooooo smoothe. \0/

Typical they only support old versions!

I on;t spend all my time reading specs but new ?? was coming and just happend to find the node 14 release notes in a google. not to self - stop trying to use the ‘shiny’ lol

Thanks for your help!!!

1 Like

Ps I nealy added a log of the node version env var in the function. D’oh!

1 Like