# Dynamically add new node

**URL:** <https://discourse.nodered.org/t/dynamically-add-new-node/4614>\
**Category:** General\
**Created:** [6 November 2018 19:40 UTC](https://discourse.nodered.org/t/dynamically-add-new-node/4614 "2018-11-06T19:40:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriel-delira](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@gabriel-delira](https://discourse.nodered.org/u/gabriel-delira)\
**Post date:** [6 November 2018 19:40 UTC](https://discourse.nodered.org/t/dynamically-add-new-node/4614/1 "2018-11-06T19:40:52Z")

</div>

I have an application with embedded node-red. In the application the user register some math operations. After that, I want to create a new node and add it to the palette. There is any way to do that?

I've tried to create a new folder with 3 files on it (.js / .json / .html).  
Then, using child\_process, run npm link node-name and finally do a POST request.

```auto
            request.post('http://localhost:3000/red/nodes/'
            , { json: { module: `npm module node-red-contrib-${MathOperation}` } }        
            , function(error, response, body){
                if(error) console.log(`ERROR??? ${error}`)
                console.log(`response`, response);
                console.log(`body`, body);
            });

```

where '/red' is the admin-root as seen:

```auto
var settings = {
  httpAdminRoot: "/red",
  httpNodeRoot: "/api",
  userDir: "myUserDir"
  functionGlobalContext: { } // enables global context
};

```

---

<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:** [6 November 2018 22:16 UTC](https://discourse.nodered.org/t/dynamically-add-new-node/4614/2 "2018-11-06T22:16:26Z")

</div>

> [@gabriel-delira](#):
>
> `npm module node-red-contrib-${MathOperation}`

The `module` parameter of the request should just be the module name that will get passed to the `npm install` command.

---

<div class="post-metadata">

**Author:** ![gabriel-delira](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@gabriel-delira](https://discourse.nodered.org/u/gabriel-delira)\
**Post date:** [27 November 2018 19:36 UTC](https://discourse.nodered.org/t/dynamically-add-new-node/4614/4 "2018-11-27T19:36:39Z")

</div>

Hum... I've made this change but still no success.

```auto
request.post('http://localhost:3000/red/nodes/'
            , { json: { module: `node-red-contrib-newoNE` } }        
            , function(error, response, body){
                if(error) console.log(`ERROR??? ${error}`)
                console.log(`body`, body);
            });

```

Now I'm receiving the following warn:

```auto
27 Nov 14:25:45 - [info] Installing module: node-red-contrib-newoNE, version: latest
27 Nov 14:25:48 - [warn] Installation of module node-red-contrib-newoNE failed: module not found

```

where `newoNE` is my local new node. I don't know if it helps but bellow are the 3 files I've mentioned before:

```javascript

//----------------------- Package.JSON -----------------------

{
	"name": "node-red-contrib-newoNE",
	"version": "0.0.1",
	"main": "newoNE.js",
	"description": "A sample node math operator for node-red",
	"dependencies": {},
	"keywords": [
		"node-red"
	],
	"node-red": {
		"nodes": {
			"newoNE": "newoNE.js"
		}
	}
}

//----------------------- NODE.js -----------------------

module.exports = function(RED) {
  function LowerCaseNodeCustom(config) {
      RED.nodes.createNode(this,config);
      var node = this;
      node.on('input', function(msg) {
          msg.payload = msg.payload.toLowerCase() + "This_is_a_new_math_operator";
          node.send(msg);
      });
  }
  RED.nodes.registerType("newoNE", LowerCaseNodeCustom);
}

//----------------------- NODE.html -----------------------

<script type="text/javascript">
    RED.nodes.registerType('newoNE',{
        category: 'function',
        color: '#00ff00',
        defaults: {
            name: {value:""}
        },
        inputs:1,
        outputs:1,
        icon: "file.png",
        label: function() {
            return this.name||"newoNE";
        }
    });
</script>
<script type="text/x-red" data-template-name="newoNE">
    <div class="form-row">
        <label for="node-input-name"><i class="icon-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
</script>
<script type="text/x-red" data-help-name="newoNE">
    <p>A simple node that converts the message payloads into all lower-case characters and add This_is_a_new_math_operator</p>
</script>

```

---

<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:** [27 November 2018 20:23 UTC](https://discourse.nodered.org/t/dynamically-add-new-node/4614/5 "2018-11-27T20:23:50Z")

</div>

Have you published your module to npm or are the files locally on disk?

If they are locally on disk, then the `module` parameter you pass in the post request must be the path to wherever your module is located on disk.

---

<div class="post-metadata">

**Author:** ![gabriel-delira](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@gabriel-delira](https://discourse.nodered.org/u/gabriel-delira)\
**Post date:** [3 December 2018 18:54 UTC](https://discourse.nodered.org/t/dynamically-add-new-node/4614/6 "2018-12-03T18:54:04Z")

</div>

Thanks it is working now! Just to let you know I'm using a local module.
