netbymatt / ws4kp

WeatherStar 4000+
https://weatherstar.netbymatt.com
MIT License
263 stars 44 forks source link

syntax error: unexpected token '?' #20

Closed jdreskell closed 1 year ago

jdreskell commented 1 year ago

Hello,

Debian Testing, nodejs 12.22.5~dfsg-2~11u1

get the following error when running node index.js

/tmp/ws4kp# node index.js
/tmp/ws4kp/index.js:6
const port = process.env.WS4KP_PORT ?? 8080;
                                     ^

SyntaxError: Unexpected token '?'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47

I can set the port manually by setting const port = 8080;

But then, another error:

/tmp/ws4kp/index.js:34
if (process.env?.DIST !== '1') {
                ^

SyntaxError: Unexpected token '.'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47
netbymatt commented 1 year ago

The problem comes from this app using the nullish coalescing operator ?? and sister optional chaining operator ?. that provide a very nice way to deal with undefined, null and default values. It was implemented in Node 14 and switching to that would be the quick fix. You can find more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing including node and browser compatibility versions.

Your hard coded port number solves the first error. The second error in the if statement can be resolved by removing the ? from the if statement as well. It's just become habit for me in the last few years to be explicit about properties in objects that may not exist by using ?? .? or other methods even when it wouldn't be absolutely necessary such as in this case.

Both of the errors that you're encountering are part of the built-in server that runs the app. I looked at the other three files used by the server in ./cors and it looks like that's the only problem you'll encounter.

The actual code that runs the web site makes extensive use of the ?? and .? operators. But it's highly unlikely you're running a browser older than roughly version 80, when they received support for these operators given their aggressive auto-update mechanisms.