# 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:** 5\
**Page:** 2

<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 12:22 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/21 "2020-02-26T12:22:34Z")

</div>

I tried with following code in .js file directly .

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

return msg;

It failed to add 1 to output in generated code .

I tried with exported json with output set to 1 ,but it did not worked at all even though it generated code without output set.

It will good if we can keep a sample code to pass in the documentation .

My NR version

26 Feb 17:22:06 - [info] Node-RED version: v1.0.3  
26 Feb 17:22:06 - [info] Node.js version: v12.16.1  
26 Feb 17:22:06 - [info] Windows\_NT 6.3.9600 x64 LE

Node version is v12.16.1

Also without passing --name in command it does not work at all ,ie it throw error as I mentioned before.

Following doc says to export as .js ,I was not able to export as .js it comes as .json .

[https://github.com/node-red/node-red-nodegen](https://github.com/node-red/node-red-nodegen)

I think the module is not written for latest NR

Can I use a code which need another module using require and generate a node?.

Thank you for your support.

---

<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:** [26 February 2020 12:27 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/22 "2020-02-26T12:27:29Z")

</div>

If you create your node manually (as per nodes - [https://nodered.org/docs/creating-nodes/first-node](https://nodered.org/docs/creating-nodes/first-node)) you can add required packages to your package.json.

nodegen works with latest Node-RED. When you say you are adding code to .js file directly - that is not the way it is supposed to work. As previously point out - you create a function node in Node-RED then save it to the library (book icon on the node config page) - then point nodegen at that file in the library.

the two methods of creating nodes cannot be mixed. (At least not easily).

---

<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:** [26 February 2020 12:30 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/23 "2020-02-26T12:30:55Z")

</div>

@josekavunkal this post from @zenofmud showed how you save the Function node to your library to get the file that nodegen works with: [Is there a simple way to generate a custom node from .js file](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/7)

You do _not_ export the whole node. You do _not_ hand-create a .js file, unless you also add the extra meta-data lines that are shown in the post I link to.

I do think that nodegen _could_ be updated to work with the JSON export of a Function node, because it will contain all of the required information. But that is a separate discussion.

---

<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 14:27 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/24 "2020-02-26T14:27:17Z")

</div>

Everything worked .  
Thank you so much . .  
It will be helpful if we have a documentation with sample code and UI screen shots .  
I was using Node-red for many years ,but did not realize that there is a book icon to save to .js .

My next step is to add an a require statement and use code from another library .

Thanks Again,

Jose

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 April 2020 14:27 UTC](https://discourse.nodered.org/t/is-there-a-simple-way-to-generate-a-custom-node-from-js-file/22096/25 "2020-04-26T14:27:27Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.

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