Is there a simple way to generate a custom node from .js file

Is there a simple way to generate a custom node from .js file.
I tried node-red-nodegen in vain .getting following error.
Error: TypeError: Cannot read property 'match' of undefined

My plan is to have a library of nodes / subflows which can be shared among many users.

2 Likes

Not that simply. nodegen works either from an existing function node - or a swagger definition file - or a web of things definition file. There are plans to try to make subflows exportable as nodes also - but that is still a plan. Until then, you will need to do the wrapping / creation yourself. https://nodered.org/docs/creating-nodes/first-node

I tried node-red-nodegen lower-case.js
But I am getting Error: TypeError: Cannot read property 'match' of undefined.
the .js file have following code

module.exports = function(RED) {
function LowerCaseNode(config) {
RED.nodes.createNode(this,config);
var node = this;
node.nodeID="JOSE_LOWERCASE";
node.on('input', function(msg) {

        msg.payload = msg.payload.toLowerCase();
        node.send(msg);
		
    });
}
RED.nodes.registerType("lower-case",LowerCaseNode);

}
This should work?

No it won't.

As Dave said, nodegen takes the code from a Function node and creates a properly packaged node.

You are giving it the full JavaScript of a node - it doesn't know what to do with that.

Thank you Nick for quick response.

I tried the following code .
msg.payload = msg.payload.toLowerCase();
return msg;

Error: TypeError: Cannot read property 'match' of undefined.

I feel this is a documentation issue or documentation can be improved.

The above doc says we can run as follows.
node-red-nodegen ~/.node-red/lib/function/lower-case.js

But this never worked for me and it worked only when I added --name mynode

Two things
(1) You need to create the .js file in an editor outside node-red. The file must have a name comment line and an outputs comment line as follows (spaces are required)

// name: lowercase
// outputs: 1
msg.payload = msg.payload.toLowerCase();
return msg;

(2) the explaination in the documentation is incorrect since the export (as far as I can see) only saves json files. I've added a note in one of the closed incidents in node-red-nodegen's GitHub page

Thanks to @knolleary for pointing out how to do the export.
Screen Shot 2020-02-23 at 8.27.25 AM

and you will get this:

// name: Lower case
// outputs: 1


msg.payload = msg.payload.toLowerCase();
return msg;

Yes it is confusing. If node owner is listening to this discussion please fix this
.issue. I lost 2 days.

1 Like

I'm writting a PR to fix the documentation.

hmm - If I save a function as it says then run the command as it says - it creates a node OK... admittedly it calls it node-red-contrib-unnamed-function but is created in the directory you run the command in... so should be easy to find and edit from there.
But yes adding the name parameter up front would make it easier, and less editing later.

Hi Jose, i noticed you had commented on another thread.
This works for me on Windows 7. Only the other day i made a couple of private nodes.

The 2 initial problems i had were :

  1. When you save to library only name the file don't create sub folder. \functions\filename.js.

  2. Separators backslash

node-red-nodegen C:\Users\PC-User.node-red\lib\functions\lower-case.js

I am using windows 8.1. Without passing name it didn't work for me. Also i tried in linux, did not work without name. Again in window 8.1 it appeared in palette manager but not in nodes panel where all nodes are listed. This is super node for my need, but these issues stops me from using, still struggling to make it work.

HI Jose.

I think you did some contract work for us a few years ago.

If you don't publish the nodes to NPM and install through the package manager pallet in node red, your custom made nodes do not get added to the package.js in the node red folder. I think the purpose of this is to stop malware sneaking into your node red instance.
So your home made nodes wont be picked up.

To get around this you have to add your custom node to the package.js file in the node red folder.

Add your node name and make sure the versioning is correct.
Some times its better to first add the node details to the package.js then add your node-red-contrib-MyHomeMadeNode to the relevant node red folder.

Restart node red it should be good to go.

Tip, when using nodered-gen tool, give the function block a name, the tool will use this name as the package name.

the name for the node is taken from the name you use for the function block.

Nice to hear from you, see the world is so small .Thank you for reminding me ,

.If we follow the steps in
https://nodered.org/docs/creating-nodes/first-node#creating-a-simple-node and copy the created folder

to node-modules it works .I was trying the same with generated node .

When I copy the generated folder to node-modules ,palette manager says that it is installed .But it is not in node list or nodes panel in left.

Without a name passed it never worked in windows 8.1 and in the AMI linux (AWS).

May be some OS related issue.

Thanks to all for the support.

Regards,

Jose

1 Like

Did you follow my steps ? You have to manually add the dependency to the node to the Node-Red Package.js file ?
When i say manually i mean you have to physically type/enter the node details to that file.

Regards

There is some problem in the way it is generated .
I have create one custom node manually with required files and published it to my npm .

Same I have done with generated node.

I have done npm install for both on .node-red folder and I can see that both modules got added to node-modules.

The generated node did not appeared in node palette but other one came.It came in palette manager,

The file you mentioned automatically got edited during install .

Waiting for another good day.:slight_smile:

I just used node-red-nodegen to generate a node from a function node and after following all the steps and stop/starting node-red, the node shows up in node-red.

Make sure you are using a name that is not already a existing node.

2 Likes

After struggling for some days ,i caught the problem .

Here is the section of HTML generated ,there is no value for
outputs:, .

If I replace
outputs:, to
outputs:1, it works.

RED.nodes.registerType('example-mynode',{
color: '#C0DEED',
category: 'function',
defaults: {
name: { value: '' },
},
inputs:1,
outputs: ,
label: function() {
return this.name || 'example-mynode';
},
icon: 'icon.png',
align: 'left'

My mynode.js
input is tiny as shown below.

msg.payload = msg.payload.toLowerCase();
return msg;

Any clue on why this happens?

Command I used is as follows.

node-red-nodegen mynode.js --name example-mynode

Thanks,

Jose

Jose, are you starting by putting that code

msg.payload = msg.payload.toLowerCase();
return msg;

in a function node in the NR editor and have you got output set to 1?

Also, what versions of NR and node.js are you running? (see start up log)