# Error: Cannot find module 'mqtt'

**URL:** <https://discourse.nodered.org/t/error-cannot-find-module-mqtt/48581>\
**Category:** General\
**Created:** [19 July 2021 11:01 UTC](https://discourse.nodered.org/t/error-cannot-find-module-mqtt/48581 "2021-07-19T11:01:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xecu114](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xecu114/32/45165_2.png) [@Xecu114](https://discourse.nodered.org/u/Xecu114)\
**Post date:** [19 July 2021 11:01 UTC](https://discourse.nodered.org/t/error-cannot-find-module-mqtt/48581/1 "2021-07-19T11:01:56Z")

</div>

Hello,

i try to create a custom node and use the mqtt js library (is also used by the mqtt nodes from node-red).  
The big problem is:  
"Cannot find module 'mqtt'"  
I tried following things:  
_npm install mqtt --save_  
_npm install mqtt -g_  
and if i look for mqtt it is listed both in my project folder as in the global folder...  
I also added mqtt to the depndencies but nothing worked...  
Does anyone have any ideas what could be the problem?  
 ![Screenshot 2021-07-19 121237](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe4c6ff1f83818e75f6fe3a55a60323c0f34c78f.png)

 ![Screenshot 2021-07-19 121114](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/8/68880911983b098e141bd8aa605f1ceeca6ad1e4.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 July 2021 11:23 UTC](https://discourse.nodered.org/t/error-cannot-find-module-mqtt/48581/2 "2021-07-19T11:23:14Z")

</div>

If you need a module then that should be declared in the node's package.json. Then when you install the node, npm will automatically install the dependency (if necessary) and make it available.

---

<div class="post-metadata">

**Author:** ![Xecu114](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xecu114/32/45165_2.png) [@Xecu114](https://discourse.nodered.org/u/Xecu114)\
**Post date:** [19 July 2021 11:32 UTC](https://discourse.nodered.org/t/error-cannot-find-module-mqtt/48581/3 "2021-07-19T11:32:17Z")

</div>

it is in my dependencies... all modules i try to use are not working...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca4e21769a7f9e22b4c43d27ac21b98296fa8b02.png)  
and i also changed the version number bbut didn't work either... i think there is a problem that my custom nodes can't access the modules in the roaming folder... but i have no idea how to fix this.  
My Node-RED Project is located here: C:\Users\JSHR.node-red  
My npm files and node-red it self are located: here C:\Users\JSHR\AppData\Roaming\npm

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 July 2021 11:44 UTC](https://discourse.nodered.org/t/error-cannot-find-module-mqtt/48581/4 "2021-07-19T11:44:01Z")

</div>

> [@Xecu114](#):
>
> npm install mqtt --save

From inside your custom node path?

> [@Xecu114](#):
>
> npm install mqtt -g

Dont do that.

> [@Xecu114](#):
>
> and if i look for mqtt it is listed both in my project folder as in the global folder.

Because you did `npm install mqtt -g`

* * *

You should simply...

- Setup your custom nodes project folder with necessary files (package.json, your-node.js, your-node.html)
- For any module that is required by your contrib node, from the base path of your custom node (e.g. `C:\my-projects\my-contrib-node`) install the requires using `npm -i mqtt` (or whatever)
- Ensure your project folder has a `.gitignore` file and exclude `node_modules` etc

Once project is ready to test, in your node-red folder (e.g. `cd c:\users\JSHR\.node-red`), install your node...

- `npm install C:/my-projects/my-contrib-node`

Restart node-red after every change to your source.

* * *

From that point on, `C:/my-projects\my-contrib-node` will be sym-linked to `c:\users\JSHR\.node-red\node_modules\my-contrib-node`. That means any files you change/delete in `c:\users\JSHR\.node-red\node_modules\my-contrib-node` will be changed/deleted from `C:/my-projects\my-contrib-node`

* * *

alternatively, if you want step debug in vscode - [follow this](https://discourse.nodered.org/t/how-to-proper-debug-custom-node/9308/13)

---

<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 August 2021 11:44 UTC](https://discourse.nodered.org/t/error-cannot-find-module-mqtt/48581/5 "2021-08-02T11:44:17Z")

</div>

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