# Node-RED node installed as dependency requires restart

**URL:** https://discourse.nodered.org/t/node-red-node-installed-as-dependency-requires-restart/56647
**Category:** Developing Nodes
**Created:** [14 January 2022 08:53 UTC](https://discourse.nodered.org/t/node-red-node-installed-as-dependency-requires-restart/56647 "2022-01-14T08:53:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sverre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sverre/32/54040_2.png) [@sverre](https://discourse.nodered.org/u/sverre)
#### Post date: [14 January 2022 08:53 UTC](https://discourse.nodered.org/t/node-red-node-installed-as-dependency-requires-restart/56647/1 "2022-01-14T08:53:45Z")

</div>

I would like to install common Node-RED "support nodes" as a dependency of other Node-RED nodes I'm developing. I've confirmed that a dependency node is installed when installing the dependent node via the palette manager, but Node-RED has to be restarted for the dependency node to be available, just as if I had run `npm install` manually.

Is this a design choice, or is it just because the dependency is installed by `npm` in the background, and Node-RED doesn't know about dependencies? Should I bother to make a feature request for Node-RED to detect such modules and load them without a restart?

Btw; I read through all the (seemingly relevant) posts yielded by a search for "dependencies" under _reating Nodes_ before posting this.

_Edit:_ After a quick look at the code, I can see that Node-RED at least _knows_ about dependencies... Not yet sure what it does with that knowledge, though.

---

<div class="post-metadata">

### Author: ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)
#### Post date: [14 January 2022 10:21 UTC](https://discourse.nodered.org/t/node-red-node-installed-as-dependency-requires-restart/56647/2 "2022-01-14T10:21:31Z")

</div>

Can you provide an example of what you are asking about here?

A link to the node you are developing so we can see the `package.json`

And how are you seeing the problem, is it throwing an exception due to not being able to find a node?

EDIT:  
OK, I think I've worked out what you mean, this will because NR only scans the explicitly installed package for nodes, not a full rescan of the `node_modules` directory. This scanning has a reasonably high overhead which is why it is directed.

This would introduce the possibility of meta nodes, that just pull in a collection of other nodes, but also begs the question to if your nodes really need to be separate or are the dependencies 3rd party?

---

<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: [14 January 2022 10:44 UTC](https://discourse.nodered.org/t/node-red-node-installed-as-dependency-requires-restart/56647/3 "2022-01-14T10:44:45Z")

</div>

There is an item on the backlog that covers this and explains why it doesn't work today.

> <https://github.com/node-red/node-red/issues/569>
>
> Node-red scans for \`node\_modules\` directories starting from its install location… and working up the tree to the root path. When it finds such a directory, it examines the \`package.json\` files of the modules in the directories immediately below; it doesn't scan deeper for additional \`node\_module\` directories.
> 
> This means a node module cannot declare another node module as a dependency and have it load properly - which is a reasonable thing to allow.
> \- when a valid node-red node module is discovered, scan its \`node\_modules\` directory
> \- ensure we are recording the version number of node modules
> \- if a node module is found that we have already loaded:
> - if the version number matches, skip it and carry on.
> - if the version numbers don't match, refuse to load it (and if this was in a node module's node\_module directory, refuse to load the top level module as its dependency cannot be satisfied)

---

<div class="post-metadata">

### Author: ![sverre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sverre/32/54040_2.png) [@sverre](https://discourse.nodered.org/u/sverre)
#### Post date: [14 January 2022 10:51 UTC](https://discourse.nodered.org/t/node-red-node-installed-as-dependency-requires-restart/56647/4 "2022-01-14T10:51:42Z")

</div>

They don't _need_ to be separate, but it seems tidier to publish them separately than bundled together. So, say _nodeA_ and _nodeB_ both can use a _postProcessNode_. If I publish _nodeA_ and _nodeB_ separately, my options are then either:

- include _postProcessNode_ in both packages.
- publish _postProcessNode_ as a separate package.

If published separately, it would be nice if it didn't have to be manually installed.

Anyway, when a node is installed from the palette manager, it's immediately added to the palette. I figured the installation logic could do whatever happens there to the dependencies as well (if appropriate).

Now off to read the linked backlog...

_Edited for clarity_  
_(and for spelling)_

---

<div class="post-metadata">

### Author: ![sverre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sverre/32/54040_2.png) [@sverre](https://discourse.nodered.org/u/sverre)
#### Post date: [14 January 2022 12:22 UTC](https://discourse.nodered.org/t/node-red-node-installed-as-dependency-requires-restart/56647/5 "2022-01-14T12:22:22Z")

</div>

**Huzzah** , I think it works! Still requires a browser refresh, but that's much better than a restart.

I had found some code for loading dependencies (in `loader.js` - `addModule()`), but I never saw any output from this debug line:

```javascript
log.debug(`Loading dependencies for ${module}`)

```

Digging further, in `localfilesystem.js` - `convertModuleFileListToObject()`:

```javascript
        if (moduleFile.package['node-red'].dependencies) {
            nodeList[moduleFile.package.name].dependencies = moduleFile.package['node-red'].dependencies;
        }

```

So `node-red/dependencies`, not the root (npm) `dependencies`.

From a 2017 comment in the backlog ([Allow node modules to have node modules as dependencies · Issue #569 · node-red/node-red · GitHub](https://github.com/node-red/node-red/issues/569#issuecomment-318350041)):

> Is there any way to declare node-red-node-dependencies.  
> Perhaps in the package.json. Something like this:

```json
"dependencies": {
    "nodered-node-serialport": "^0.0.1"
},
"keywords": ["node-red"],
"node-red" : {
    "nodes": {
        "sample": "sample/sample.js"
    },
    "dependencies": {
        "nodered-node-serialport": "^0.0.1"
    }
}

```

I'm guessing `node-red/dependencies` was just speculation at this point? Anyway, I found a reference to it in the docs for [Packaging a Subflow as a module](https://nodered.org/docs/creating-nodes/subflow-modules#adding-dependencies), confirming my understanding of the code from `localfilesystem.js`, and gave it a try. Found the commit for the dependency handling in `loader.js` - [Handle subflow modules with their own npm dependencies](https://github.com/node-red/node-red/pull/2690/commits/da96c85d325bce196ba5b947643b470bbaa27618). So the feature was added for a different use case, but unless there's plans to drastically alter (or remove) it, I guess it's OK to use it for this as well?

It would be nice if the dependency nodes were visible in the palette without a browser refresh, but that'll be a problem for another day.

_Edit: added a couple of lines from the backlog comment_

---

<div class="post-metadata">

### Author: ![sverre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sverre/32/54040_2.png) [@sverre](https://discourse.nodered.org/u/sverre)
#### Post date: [1 February 2022 16:02 UTC](https://discourse.nodered.org/t/node-red-node-installed-as-dependency-requires-restart/56647/6 "2022-02-01T16:02:14Z")

</div>

I tested this back to Node-RED 1.3[.1], where **Subflow Modules** were added.

The nodes have been released as [@gapit/node-red-contrib-gapit-snmp](https://flows.nodered.org/node/@gapit/node-red-contrib-gapit-snmp) and [@gapit/node-red-contrib-gapit-results](https://flows.nodered.org/node/@gapit/node-red-contrib-gapit-results), the latter is installed as a dependency. I included a note in the README about refreshing the browser to see the second node in the palette.

---

<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: [2 April 2022 16:02 UTC](https://discourse.nodered.org/t/node-red-node-installed-as-dependency-requires-restart/56647/7 "2022-04-02T16:02:48Z")

</div>

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