Avoid "node" as a variable name in functions

It took me a few minutes to sort this one out. The code in my (demo) function node is:

node.warn('hello world');
var node = 'hello world';
node.warn('hello world');
return msg;

The third line throws an error, "TypeError: node.warn is not a function"

I guess it's clear what is happening. There probably is no need for a warning in the documentation, unless there are other keywords that should be avoided. (I haven't had time to think about it.)

1 Like

I beleive I bumped into this at one point and spent some time pulling my hair out :grin:

:rofl:

You might want to avoid RED as well :grinning:

3 Likes

I'd suggest avoiding everything we document as being already-defined in the Function node - https://nodered.org/docs/user-guide/writing-functions#api-reference

1 Like

Sorry but this is pure JS .... Node-RED exposes a variable/object named "node"... if you redefine it as a string, of course the original functions/methods/properties will be deleted and it won't work ...
Also do not redefine RED or undefined and many others...

Well yes, that's why I said it's clear what's happening, but no it's also a Node-RED issue since it was not obvious to me exactly what objects are exposed in the function node sandbox. The page @knolleary linked is helpful, assuming it is complete and stable.

This may confuse some folks. "undefined and many others" presumably refers to JS reserved names. On the other hand, RED is a NR-defined object, and as far as I can see from the documentation, the only others like it are node, context, flow, and global. Please let me know if I have missed something.

1 Like

The link I gave you lists all of the pre-defined variables within the Function node.

I think it is reasonable for you to assume it is complete and stable.

Thanks! I didn't mean to imply otherwise. :slightly_smiling_face: