JavaScript Method Node.js Compatibility

Hey there …

The replaceAll( ) JS method is working as expected on frontend (Chrome browser), but it’s not running on the backend, I get an error variable.replaceAll() is not a function . I’ve checked the compatibility of the method on Node.js .

const text = 'A paragraph here';
const removeSpaces = text.replaceAll(' ', '');

console.log(removeSpaces);
/* Result:
Backend: text.replaceAll is not a function.
Frontend: "Aparagraphhere"
*/

As a workaround, I used the regular replace( ) JS method.

// Backend:
let text = 'A paragraph here';
While (text.includes(' ')) { text = text.replace(' ') }

Any idea why? And what can I use instead?
Thanks in advance.

Not sure, but it might be that it’s not available in nodeJS. I did a quick search and couldn’t find it in nodeJS.

Thank you for replying, what about the compatibility in the replaceAll( ) method documentation?

What is the node version on Wix servers?

Not sure about Node compatibility @ahmadnasriya , but what you can use is instead is the replace function, with a global regex. Much faster and cleaner than the while.

So for your code it would be:

text = text.replace(/ /g,'');

Thank you for the tip Chris , it’s absolutely much better, much appreciated!

@ahmadnasriya Of course man, no worries.

1 Like

@ahmadnasriya If believe Wix is on Node 12 not 15

That’s good to know for the future, thank you.