String replaceAll does not work in function Node

Hi Team,

I am using the string .replace feature without problem, but .replaceAll throws an error:

TypeError: item.title.replaceAll is not a function
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:11:36
    at Object.base.apply (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/contextify.js:228:34)
    at Array.forEach (<anonymous>)
    at Object.base.apply (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/contextify.js:621:32)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:5:7
    at Object.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:19:18)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1167:29)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:81:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:508:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:442:62

I will say I can daisy chain .replace(" ", “w”).replace(“g”,“t”) all day long… so thats good.

Hi @wsargent,

It looks like the problem here is NodeJS before version 15 doesn’t have the replaceAll() function and the n8n docker images appear to use 14.15.

In theory if you run it on your own machine and you have version 15 of Node installed it should work.

2 Likes

Hey @wsargent,

As Jon mentioned, it because n8n uses node v14.15. Can regular expression be helpful for you?

Thanks @Jon, that’s really helpful. I thought I was losing my mind reading the node.js docs! :slight_smile: I’m on cloud.

1 Like

@harshil1712 Thanks, that looks like the answer!

For reference to others, the suggestion if I wanted to replace all dashes with an underscore, do this:

.replace(/-/g,"")
.replace(/\s/g,"
") replaces all spaces with _

Useful tutorial here:
Node.js — String Replace All Appearances (futurestud.io)

2 Likes

Thank you for sharing the solution!

If someone wants to test out regex, I use - https://regex101.com/