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

**URL:** <https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096>\
**Category:** General\
**Created:** [22 February 2020 14:38 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096 "2020-02-22T14:38:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![josekavunkal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josekavunkal/32/18505_2.png) [@josekavunkal](https://discourse.nodered.org/u/josekavunkal)\
**Post date:** [22 February 2020 14:38 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/1 "2020-02-22T14:38:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 February 2020 14:56 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/2 "2020-02-22T14:56:44Z")

</div>

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](https://nodered.org/docs/creating-nodes/first-node)

---

<div class="post-metadata">

**Author:** ![josekavunkal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josekavunkal/32/18505_2.png) [@josekavunkal](https://discourse.nodered.org/u/josekavunkal)\
**Post date:** [22 February 2020 15:18 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/3 "2020-02-22T15:18:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 February 2020 22:35 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/4 "2020-02-22T22:35:55Z")

</div>

> [@josekavunkal](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![josekavunkal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josekavunkal/32/18505_2.png) [@josekavunkal](https://discourse.nodered.org/u/josekavunkal)\
**Post date:** [23 February 2020 02:42 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/5 "2020-02-23T02:42:12Z")

</div>

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.

> **[node-red/node-red-nodegen](https://github.com/node-red/node-red-nodegen)**
>
> Command line tool to convert Node-RED function node, Swagger document, or WoT Thing Description to a custom Node. - node-red/node-red-nodegen

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 February 2020 10:57 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/6 "2020-02-23T10:57:42Z")

</div>

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)

```auto
// 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](https://github.com/node-red/node-red-nodegen/issues/99) in node-red-nodegen's GitHub page

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 February 2020 13:28 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/7 "2020-02-23T13:28:17Z")

</div>

Thanks to @knolleary for pointing out how to do the export.  
 ![Screen Shot 2020-02-23 at 8.27.25 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/1/019a816e466c48d9c2a8071c51f1912e57897489.png)

and you will get this:

```auto
// name: Lower case
// outputs: 1

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

```

---

<div class="post-metadata">

**Author:** ![josekavunkal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josekavunkal/32/18505_2.png) [@josekavunkal](https://discourse.nodered.org/u/josekavunkal)\
**Post date:** [23 February 2020 15:22 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/8 "2020-02-23T15:22:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 February 2020 16:10 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/9 "2020-02-23T16:10:47Z")

</div>

I'm writting a PR to fix the documentation.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [23 February 2020 17:55 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/10 "2020-02-23T17:55:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![smithtek](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smithtek/32/17411_2.png) [@smithtek](https://discourse.nodered.org/u/smithtek)\
**Post date:** [24 February 2020 01:26 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/11 "2020-02-24T01:26:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josekavunkal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josekavunkal/32/18505_2.png) [@josekavunkal](https://discourse.nodered.org/u/josekavunkal)\
**Post date:** [24 February 2020 01:58 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/12 "2020-02-24T01:58:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![smithtek](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smithtek/32/17411_2.png) [@smithtek](https://discourse.nodered.org/u/smithtek)\
**Post date:** [24 February 2020 04:11 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/13 "2020-02-24T04:11:27Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/6/b6c6f2a427ccc105e87360fc338b216bb6b76b97.png)

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.

---

<div class="post-metadata">

**Author:** ![smithtek](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smithtek/32/17411_2.png) [@smithtek](https://discourse.nodered.org/u/smithtek)\
**Post date:** [24 February 2020 04:13 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/14 "2020-02-24T04:13:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josekavunkal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josekavunkal/32/18505_2.png) [@josekavunkal](https://discourse.nodered.org/u/josekavunkal)\
**Post date:** [24 February 2020 04:47 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/15 "2020-02-24T04:47:24Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![smithtek](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smithtek/32/17411_2.png) [@smithtek](https://discourse.nodered.org/u/smithtek)\
**Post date:** [24 February 2020 05:06 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/16 "2020-02-24T05:06:31Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/b/ebe11c91b2968b9bbfd10633df7cab576c0871d3.png)

---

<div class="post-metadata">

**Author:** ![josekavunkal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josekavunkal/32/18505_2.png) [@josekavunkal](https://discourse.nodered.org/u/josekavunkal)\
**Post date:** [24 February 2020 10:55 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/17 "2020-02-24T10:55:37Z")

</div>

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.🙂

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [24 February 2020 14:26 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/18 "2020-02-24T14:26:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![josekavunkal](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josekavunkal/32/18505_2.png) [@josekavunkal](https://discourse.nodered.org/u/josekavunkal)\
**Post date:** [26 February 2020 10:06 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/19 "2020-02-26T10:06:05Z")

</div>

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](http://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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 February 2020 11:06 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/20 "2020-02-26T11:06:37Z")

</div>

Jose, are you starting by putting that code

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

```

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

 ![Screen Shot 2020-02-26 at 6.02.42 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/878d968c6f5f285610ae03a65f1803865676c0c7.png)

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

[Next page](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096.md?page=2)
