0

I recently tried to upload some of my svelte-kit code to a cPanel node.js app, but got an error saying that "??=" was an unexpected token. I was looking through the code, and found the line causing the error const component = async () => component_cache ??= (await import('./layout.svelte-32c85b96.mjs')).default;. I was wondering how to replace this as the node.js version I can use is 14. I tried looking up "??=" on Google but found literally nothing.

codingtuba
  • 366
  • 2
  • 13

1 Answers1

1

What the ??= (Nullish coalescing assignment) operator does is assign a value to the variable if it is undefined. For example,

let a; let loga = ()=>console.log(a);
loga() // => undefined
a ??= 1 // a is set to 1
loga() // => 1
a ??= 2 // a is not set to 2 as it is already 1
loga() // => 1
a = undefined
a ??= 2 // a is now set to 2 as it is now undefined
loga() // => 2

So, if you want to fix the error then you should probably just use a binary operator instead of a ??=:

let a;
a == null ? (a = "new value") : ("")
// vs:
// a ??= "new value"
codingtuba
  • 366
  • 2
  • 13