# MQTT node disconnected

**URL:** <https://discourse.nodered.org/t/mqtt-node-disconnected/35724>\
**Category:** General\
**Created:** [11 November 2020 08:32 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724 "2020-11-11T08:32:32Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![thecoder](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@thecoder](https://discourse.nodered.org/u/thecoder)\
**Post date:** [11 November 2020 08:32 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/1 "2020-11-11T08:32:32Z")

</div>

My flow shows the MQTT nodes as disconnected, I'm running mosquitto. It runs on port 1883 and the MQTT nodes are configured to connect in localhost on port 1883

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2d8d1c79547673ce6af893d1bff801489d718650.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/199fe6f3f05ccad9ab221458869d02faa32d31a9.png)

When I run Node-red "node-red", I get this error at the terminal:

```auto
11 Nov 09:54:42 - [info] Starting flows
ReferenceError: URL is not defined
    at Object.connect (/usr/local/lib/node_modules/node-red/node_modules/mqtt/lib/connect/index.js:64:18)
    at MQTTBrokerNode.connect (/usr/local/lib/node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js:245:40)
    at MQTTBrokerNode.register (/usr/local/lib/node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js:221:22)
    at new MQTTInNode (/usr/local/lib/node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js:421:33)
    at Object.createNode (/usr/local/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/flows/util.js:493:31)
    at Flow.start (/usr/local/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/flows/Flow.js:194:48)
    at start (/usr/local/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/flows/index.js:345:33)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

```

Should I remove mosquitto and install it again?

---

<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:** [11 November 2020 09:37 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/2 "2020-11-11T09:37:49Z")

</div>

> [@thecoder](#):
>
> Should I remove mosquitto and install it again?

No, that won't help.  
Start node-red in a terminal and post the full log here, from the start up to the error message please.

---

<div class="post-metadata">

**Author:** ![thecoder](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@thecoder](https://discourse.nodered.org/u/thecoder)\
**Post date:** [11 November 2020 10:05 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/3 "2020-11-11T10:05:17Z")

</div>

```auto
user@user-VirtualBox:~$ node-red
11 Nov 11:03:37 - [info] 

Welcome to Node-RED
===================

11 Nov 11:03:37 - [info] Node-RED version: v1.2.3
11 Nov 11:03:37 - [info] Node.js version: v8.10.0
11 Nov 11:03:37 - [info] Linux 5.4.0-52-generic x64 LE
11 Nov 11:03:37 - [info] Loading palette nodes
11 Nov 11:03:39 - [info] Settings file : /home/user/.node-red/settings.js
11 Nov 11:03:39 - [info] Context store : 'default' [module=memory]
11 Nov 11:03:39 - [info] User directory : /home/user/.node-red
11 Nov 11:03:39 - [warn] Projects disabled : editorTheme.projects.enabled=false
11 Nov 11:03:39 - [info] Flows file : /home/user/.node-red/flows_user-VirtualBox.json
11 Nov 11:03:39 - [info] Server now running at http://127.0.0.1:1880/
11 Nov 11:03:39 - [warn] 

---------------------------------------------------------------------
Your flow credentials file is encrypted using a system-generated key.

If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.

You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
---------------------------------------------------------------------

11 Nov 11:03:39 - [info] Starting flows
ReferenceError: URL is not defined
    at Object.connect (/usr/local/lib/node_modules/node-red/node_modules/mqtt/lib/connect/index.js:64:18)
    at MQTTBrokerNode.connect (/usr/local/lib/node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js:245:40)
    at MQTTBrokerNode.register (/usr/local/lib/node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js:221:22)
    at new MQTTInNode (/usr/local/lib/node_modules/node-red/node_modules/@node-red/nodes/core/network/10-mqtt.js:421:33)
    at Object.createNode (/usr/local/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/flows/util.js:493:31)
    at Flow.start (/usr/local/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/flows/Flow.js:194:48)
    at start (/usr/local/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/flows/index.js:345:33)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
11 Nov 11:03:39 - [info] Started flows
(node:1588) DeprecationWarning: createSession: Create Session object directly instead

```

This is the log from the terminal

---

<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:** [11 November 2020 10:10 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/4 "2020-11-11T10:10:54Z")

</div>

The problem is almost certainly the ancient version of nodejs you are using. Version 8.10 is nearly three years old and the latest release of 8.x (8.17.x I think) has been out of support for some months. At least upgrade to 8.17 and preferably to 12.x.

If you are using a Debian based OS such as Ubuntu then you can upgrade using the pi/Debian/Ubuntu script recommended in the node red docs. [https://nodered.org/docs/getting-started/raspberrypi](https://nodered.org/docs/getting-started/raspberrypi)

---

<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:** [11 November 2020 10:16 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/5 "2020-11-11T10:16:11Z")

</div>

> [@thecoder](#):
>
> `URL is not defined`

This [might be an issue](https://github.com/mqttjs/MQTT.js/issues?q=URL+is+not+defined) with latest MQTT.js - there has been lots of activity on that project recently.

You could try modifying package.json in .node-red and setting mqtt version to an earlier version (see versions [here](https://www.npmjs.com/package/mqtt?activeTab=versions)) then run `npm install`

I would try [4.1.0](https://www.npmjs.com/package/mqtt/v/4.1.0) or [2.18.8](https://www.npmjs.com/package/mqtt/v/2.18.8)

---

<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:** [11 November 2020 10:39 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/6 "2020-11-11T10:39:36Z")

</div>

Isn't it the mqtt library installed with the node-red core that is giving the problem? If so then that is installed globally. Running, from the .node-red folder,  
`npm list mqtt`  
will show if it is installed locally (in .node-red/node\_modules), or  
`npm list -g mqtt`  
will show if it is installed globally, and will show the version. I get

```auto
npm list -g mqtt
/usr/lib
└─┬ node-red@1.2.3
  └─┬ @node-red/nodes@1.2.3
    └── mqtt@4.2.4 

```

---

<div class="post-metadata">

**Author:** ![thecoder](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@thecoder](https://discourse.nodered.org/u/thecoder)\
**Post date:** [11 November 2020 10:46 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/7 "2020-11-11T10:46:25Z")

</div>

I got this inside .node-red

```auto
user@user-VirtualBox:~/.node-red$ npm list mqtt
node-red-project@0.0.1 /home/user/.node-red
└── (empty)

user@user-VirtualBox:~/.node-red$ mqtt list -g mqtt

Command 'mqtt' not found, did you mean:

  command 'mutt' from snap mutt (1-14-7-rel)
  command 'mutt' from deb mutt
  command 'mtt' from deb xawtv

See 'snap info <snapname>' for additional versions.

```

However, node-red works

---

<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:** [11 November 2020 10:52 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/8 "2020-11-11T10:52:49Z")

</div>

Try adding `-g` to that npm command.

---

<div class="post-metadata">

**Author:** ![thecoder](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@thecoder](https://discourse.nodered.org/u/thecoder)\
**Post date:** [11 November 2020 10:58 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/9 "2020-11-11T10:58:24Z")

</div>

user@user-VirtualBox:~/.node-red$ npm -g list mqtt  
/home/user/.nvm/versions/node/v14.4.0/lib  
└── (empty)

---

<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:** [11 November 2020 11:28 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/10 "2020-11-11T11:28:55Z")

</div>

> [@thecoder](#):
>
> `mqtt list -g mqtt`

Sorry, typo on my part, should have been `npm list -g mqtt` (which is actually the command I ran). I will correct my post.

---

<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:** [11 November 2020 11:31 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/11 "2020-11-11T11:31:28Z")

</div>

> [@thecoder](#):
>
> /home/user/.nvm/versions/node/v14.4.0/lib

You shouldn't be using nvm unless you know what you are doing. I suggest uninstalling that, uninstalling nodejs and npm and running the script I suggested. I still think the fundamental problem is the nodejs version.

---

<div class="post-metadata">

**Author:** ![thecoder](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@thecoder](https://discourse.nodered.org/u/thecoder)\
**Post date:** [11 November 2020 11:34 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/12 "2020-11-11T11:34:39Z")

</div>

Should I uninsall npm? It is part of the command you've provided me.

About installing the latest version of nodejs, I cannot upgrade from 8 to 12 (or later versions). I've followed many tutorial but it doesn't work.

---

<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:** [11 November 2020 12:07 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/13 "2020-11-11T12:07:57Z")

</div>

Yes uninstall npm nodejs npm and (assuming you are running a Debian based distribution, then run the script.

---

<div class="post-metadata">

**Author:** ![thecoder](https://avatars.discourse-cdn.com/v4/letter/t/a87d85/32.png) [@thecoder](https://discourse.nodered.org/u/thecoder)\
**Post date:** [11 November 2020 12:09 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/14 "2020-11-11T12:09:31Z")

</div>

It is ubuntu 18.04

---

<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:** [11 November 2020 12:32 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/15 "2020-11-11T12:32:27Z")

</div>

> [@thecoder](#):
>
> ubuntu

ubuntu is debian based - run the installer as described in the docs.

---

<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:** [11 November 2020 12:49 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/16 "2020-11-11T12:49:49Z")

</div>

> [@Colin](#):
>
> uninstall npm nodejs npm

Blasted phone! Try again: nvm, nodejs, npm

---

<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:** [10 January 2021 12:49 UTC](https://discourse.nodered.org/t/mqtt-node-disconnected/35724/17 "2021-01-10T12:49:55Z")

</div>

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