Error: Cannot find module 'mqtt'

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

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.

it is in my dependencies... all modules i try to use are not working...
image
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

From inside your custom node path?

Dont do that.

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

1 Like

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