# Why multiple node\_modules directories?

**URL:** <https://discourse.nodered.org/t/why-multiple-node-modules-directories/10005>\
**Category:** General\
**Created:** [11 April 2019 02:35 UTC](https://discourse.nodered.org/t/why-multiple-node-modules-directories/10005 "2019-04-11T02:35:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nodoro](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@nodoro](https://discourse.nodered.org/u/nodoro)\
**Post date:** [11 April 2019 02:35 UTC](https://discourse.nodered.org/t/why-multiple-node-modules-directories/10005/1 "2019-04-11T02:35:25Z")

</div>

On my Raspberry Pi install, I used to find the core nodes in ' **/usr/lib/node\_modules/node-red/nodes/core**'

After running the UPGRADE command found in the [Running on Raspberry Pi](https://nodered.org/docs/hardware/raspberrypi) page, it looks like the core nodes have been moved to ' **/usr/lib/node\_modules/node-red/node\_modules/@node-red/nodes/core**'

I poked around some more and found at least three paths for node-modules on my Raspberry Pi, and it looks like a lot of modules themselves are duplicated.  
For example, there is a module folder for 'are-we-there-yet' in all of the following directories:  
**/home/pi/.node-red/node\_modules**  
**/usr/lib/node\_modules**  
**/usr/lib/node\_modules/npm/node\_modules**

Is there a legitimate reason for the multiple locations, or are some of them redundant or leftover from the upgrade? I kind of get why there is a /home/pi... directory for MY nodes, but it seems silly to maintain the installed nodes in multiple locations.

Thanks in advance for any feedback.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [12 April 2019 15:39 UTC](https://discourse.nodered.org/t/why-multiple-node-modules-directories/10005/2 "2019-04-12T15:39:13Z")

</div>

I believe this is part of the rework being done on Node-RED in preparation for the move to v1. The core is being restructured. Doubtless this will resolve itself in time.

---

<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:** [12 April 2019 16:56 UTC](https://discourse.nodered.org/t/why-multiple-node-modules-directories/10005/3 "2019-04-12T16:56:57Z")

</div>

> [@TotallyInformation](#):
>
> I believe this is part of the rework being done on Node-RED in preparation for the move to v1.

Yes and no.

The fact the core nodes have moved directory is because of the repackaging we did in 0.20.

The fact there are lots of node\_modules directories is just how npm works.

> [@nodoro](#):
>
> Is there a legitimate reason for the multiple locations,

Yes. `/usr/lib/node_modules` is the global npm module location - anything installed with `-g` as a global module.

You also then get locally installed modules - such as those you install just for node-red under `/home/pi/.node-red/node_modules`.

There are whole essays written about how `npm` lays out modules when running an install - I'm not going to go over it all, but here's the basics.

First thing to know is that when some code `requires` a module, it looks in the current directory for a `node_modules` directory, and looks in that directory for the module being loaded. If it doesn't find it, it moves to the parent directory and again looks for `node_modules` - and so on, all the way to the root of the file system.

Every module defines its own list of modules it depends on. One of the tasks `npm` has to do is to figure out how to install all of those dependencies.

In the early days of npm, it would install all dependencies below the modules own directory - so if you had module `foo` that depended on `bar`, and also module `baz` that also depended on `bar`, you'd end up with two copies of `bar`:

```auto
/usr/lib/node_modules/foo/node_modules/bar/
/usr/lib/node_modules/baz/node_modules/bar/

```

More recent versions of `npm` tries to reduce this duplication by installing dependencies as high as possible in the directory path. So you would get:

```auto
/usr/lib/node_modules/foo
/usr/lib/node_modules/baz
/usr/lib/node_modules/bar

```

However, if `foo` and `baz` depend on _different versions_ of `bar`, npm hasto put individual copies of `bar` under the `node_modules` directory of the individual modules.

So yes you can end up with lots of copies of the same module installed; but npm does a lot of work to minimise that duplication.
