# Nodered keeps restarting with processTicksAndRejections error

**URL:** <https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956>\
**Category:** General\
**Created:** [26 March 2023 10:02 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956 "2023-03-26T10:02:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![klose54](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klose54](https://discourse.nodered.org/u/klose54)\
**Post date:** [26 March 2023 10:02 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/1 "2023-03-26T10:02:36Z")

</div>

Hello everyone,

I'm actually experiencing some troubles with Nodered. Following my raspberry pi crash I needed to redo the complete setup, so I install a fresh Nodered v3 and used a flow backup that I made not long ago.

The trouble is that now a specific node is making a mess and nodered is restarting after the flow start running and the error occuring. I need to modify the flows.json to delete this node so it stops restarting.

The node I believe to get me trouble is the surepetcare one.

> # Welcome to Node-RED
> 
> ## 26 Mar 11:58:30 - [info] Node-RED version: v3.0.2 26 Mar 11:58:30 - [info] Node.js version: v16.19.1 26 Mar 11:58:30 - [info] Linux 6.1.19-v7+ arm LE 26 Mar 11:58:33 - [info] Loading palette nodes 26 Mar 11:58:39 - [info] Worldmap version 2.34.0 26 Mar 11:58:40 - [info] Dashboard version 3.4.0 started at /ui 26 Mar 11:58:40 - [info] Settings file : /home/klose54/.node-red/settings.js 26 Mar 11:58:40 - [info] Context store : 'default' [module=memory] 26 Mar 11:58:40 - [info] User directory : /home/klose54/.node-red 26 Mar 11:58:40 - [warn] Projects disabled : editorTheme.projects.enabled=false 26 Mar 11:58:40 - [info] Flows file : /home/klose54/.node-red/flows.json 26 Mar 11:58:40 - [info] Server now running at [http://127.0.0.1:1880/](http://127.0.0.1:1880/) 26 Mar 11:58:40 - [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.
> 
> 26 Mar 11:58:44 - [info] Starting flows  
> 26 Mar 11:58:44 - [info] [surepet-credentials:b5cc3c783ef12564] We have a username and password, attempting to authenticiate.

....................

> 26 Mar 11:59:16 - [red] Uncaught Exception:  
> 26 Mar 11:59:16 - [error] Error: Get state failed: unauthenticated  
> at SurePetCareClient.getState (/home/klose54/.node-red/node\_modules/sure-pet-care-client/dist/client.js:39:31)  
> at processTicksAndRejections (node:internal/process/task\_queues:96:5)

The thing is it seems to be and authentification problem, but when the node is manually called it works. Everytime I put an inject node before so I automatically call the node, it keeps getting me this error and nodered is restarting ...

Does anyone can help me ?

---

<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:** [26 March 2023 10:09 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/2 "2023-03-26T10:09:47Z")

</div>

> [@klose54](#):
>
> Uncaught Exception:  
> 26 Mar 11:59:16 - [error] Error: Get state failed: unauthenticated  
> at SurePetCareClient.getState (/home/klose54/.node-red/node\_modules/sure-pet-care-client/dist/client.js:39:31)

How are you using [sure-pet-care-client - npm](https://www.npmjs.com/package/sure-pet-care-client) ? in a function node? If so, you need to handle the error.

To start node-red to get access & fix up your flows, you can start in safe mode.

e.g. `node-red --safe`

---

<div class="post-metadata">

**Author:** ![klose54](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klose54](https://discourse.nodered.org/u/klose54)\
**Post date:** [26 March 2023 10:14 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/3 "2023-03-26T10:14:44Z")

</div>

Hello and thank you for your reply.

I'm using it as the function node with only credentials in it and getting payload as I always do. I've never made anything specific to handle errors. How should I make this ?

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

Thank you for the safe command, this will be useful.

---

<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:** [26 March 2023 10:41 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/4 "2023-03-26T10:41:55Z")

</div>

Actually that looks like a contrib node.

In short, a contrib node should NOT crash node-red, regardless of bad credentials or misconfiguration.

You should raise an issue on the repository: [Issues · jpwsutton/node-red-contrib-surepet · GitHub](https://github.com/jpwsutton/node-red-contrib-surepet/issues)

I believe the issue is they are not handling the promise .then & .catch here: [node-red-contrib-surepet/surepet.js at 548e3d50560bb9af4b4a38195c72e43495da02d0 · jpwsutton/node-red-contrib-surepet · GitHub](https://github.com/jpwsutton/node-red-contrib-surepet/blob/548e3d50560bb9af4b4a38195c72e43495da02d0/surepet.js#L47)

---

<div class="post-metadata">

**Author:** ![klose54](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klose54](https://discourse.nodered.org/u/klose54)\
**Post date:** [26 March 2023 11:01 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/6 "2023-03-26T11:01:39Z")

</div>

Thank you for your help, I raised an issue on the github.

> <https://github.com/jpwsutton/node-red-contrib-surepet/issues/1>
>
> Hello,
> 
> I'm using the contrib node in nodered for a while, and after a crash o…f my setup I had to make a complete fresh new setup with nodered v3. 
> 
> I deployed a flows backup with the node and the credentials, when using it manually it works as it as always worked, but when i make it work with an inject node with a repeat every second, it makes this error and nodered keeps ketting it and constantly reboot 
> 
> 
> \`\`\`
> Welcome to Node-RED
> 26 Mar 11:58:30 - \[info\] Node-RED version: v3.0.2
> 26 Mar 11:58:30 - \[info\] Node.js version: v16.19.1
> 26 Mar 11:58:30 - \[info\] Linux 6.1.19-v7+ arm LE
> 26 Mar 11:58:33 - \[info\] Loading palette nodes
> 26 Mar 11:58:39 - \[info\] Worldmap version 2.34.0
> 26 Mar 11:58:40 - \[info\] Dashboard version 3.4.0 started at /ui
> 26 Mar 11:58:40 - \[info\] Settings file : /home/klose54/.node-red/settings.js
> 26 Mar 11:58:40 - \[info\] Context store : 'default' \[module=memory\]
> 26 Mar 11:58:40 - \[info\] User directory : /home/klose54/.node-red
> 26 Mar 11:58:40 - \[warn\] Projects disabled : editorTheme.projects.enabled=false
> 26 Mar 11:58:40 - \[info\] Flows file : /home/klose54/.node-red/flows.json
> 26 Mar 11:58:40 - \[info\] Server now running at http://127.0.0.1:1880/
> 26 Mar 11:58:40 - \[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.
> 
> 26 Mar 11:58:44 - \[info\] Starting flows
> 26 Mar 11:58:44 - \[info\] \[surepet-credentials:b5cc3c783ef12564\] We have a username and password, attempting to authenticiate.
> \`\`\`
> 
> 
> 
> \`\`\`
> Uncaught Exception:
> 26 Mar 11:59:16 - \[error\] Error: Get state failed: unauthenticated
> at SurePetCareClient.getState (/home/klose54/.node-red/node\_modules/sure-pet-care-client/dist/client.js:39:31)
> at processTicksAndRejections (node:internal/process/task\_queues:96:5)
> \`\`\`

---

<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:** [26 March 2023 11:11 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/7 "2023-03-26T11:11:28Z")

</div>

> [@klose54](#):
>
> Everytime I put an inject node before so I automatically call the node, it keeps getting me this error and nodered is restarting

If it is only on startup that you are getting the problem, then a workaround may be to configure the inject node to trigger a little time after startup. Set it to 60 seconds initially and see what happens.

---

<div class="post-metadata">

**Author:** ![klose54](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klose54](https://discourse.nodered.org/u/klose54)\
**Post date:** [26 March 2023 12:07 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/8 "2023-03-26T12:07:58Z")

</div>

It seems to be working for now, I changed the inject nodes as you said, reboot nodered for a few times and its working fine, let's see for a longer period. Thanks for the idea.

---

<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:** [26 March 2023 12:47 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/9 "2023-03-26T12:47:34Z")

</div>

Possibly, the bug is being triggered if node red starts up before the network is connected, which is likely if you are using wifi. In the node red systemd startup script, there is a line you can uncomment which will make it wait for a time sync on boot (which means the network must be running) before starting node-red, so you could try that instead. It does have the disadvantage that if you it cannot connect then node red will never start, which can be confusing.

---

<div class="post-metadata">

**Author:** ![klose54](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klose54](https://discourse.nodered.org/u/klose54)\
**Post date:** [27 March 2023 15:35 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/10 "2023-03-27T15:35:16Z")

</div>

So since that i delayed for a few seconds the first call of this node does improve and never does the same issue again.

But now it happens a lot of time that nodered restart on this :

```auto
[red] Uncaught Exception:
[error] Error: Get state failed: received 504 Gateway Time-out
    at SurePetCareClient.getState (/home/klose54/.node-red/node_modules/sure-pet-care-client/dist/client.js:41:31)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

```

Since yesterday I have this like 5 times per hour, which can an issue. Maybe the contrib node is not working well with a new version of something ? It seems to be not updated since 3 years.

---

<div class="post-metadata">

**Author:** ![klose54](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klose54](https://discourse.nodered.org/u/klose54)\
**Post date:** [3 April 2023 16:30 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/11 "2023-04-03T16:30:39Z")

</div>

I believe that the creator of this contrib is no longer active on this one and that the github message may be a no go ...

The problem is that everytime the server seems to be down for a short time, my nodered keeps rebooting.

Is there anybody that knows what to change in this part of the code for the node to make an error message instead of crashing on a 504 gateway time-out failure ?

```auto

function SurePetCredentials(n){
        RED.nodes.createNode(this, n);
        this.username = n.username || this.credentials.username;
        this.password = n.password || this.credentials.password;
        this.client = new SurePet.SurePetCareClient();
        if (this.username && this.password){
            this.log("We have a username and password, attempting to authenticiate.")
            try{
                this.client.authenticate(this.username, this.password);
                this.status({fill:"green",shape:"dot",text:"Connected."});
            } catch (err){
                this.status({fill:"red",shape:"ring",text:err});
            }
           
        }
    }

```

---

<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:** [3 April 2023 16:47 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/12 "2023-04-03T16:47:08Z")

</div>

> [@klose54](#):
>
> ```auto
> try{
> this.client.authenticate(this.username, this.password);
> this.status({fill:"green",shape:"dot",text:"Connected."});
> } catch (err){
> this.status({fill:"red",shape:"ring",text:err});
> }
> 
> ```

Since that function is async (see docs [here](https://www.npmjs.com/package/sure-pet-care-client#authenticate))

> #### Authenticate
> 
> Authenticates the user using email and password.
> 
> client.authenticate(email: string, password: string): Promise\<void\>;

Note the returns `promise` ☝ that indicates it is async.

So, use the `await` keyword

```auto
            try{
                await this.client.authenticate(this.username, this.password);
                this.status({fill:"green",shape:"dot",text:"Connected."});
            } catch (err){
                this.status({fill:"red",shape:"ring",text:err});
            }

```

This will cause the `try ~ catch` to actually catch the error.

---

<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:** [3 April 2023 16:51 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/13 "2023-04-03T16:51:19Z")

</div>

Note also, `getPets` and `getState` are also async. They have `.then` but no `.catch` so I would also change...

```auto
        node.on('input', function(msg){

            if(config.mode === 'list_pets'){
            this.surepet.client.getPets().then((pets) =>{
                msg.payload = {
                    pets: pets
                }
                node.send(msg)
            })
        } else if(config.mode === "get_status"){
            this.surepet.client.getState().then((state) => {
                msg.payload = state;
                node.send(msg);
            })

        }

```

to

```auto
node.on('input', function (msg) {
    if (config.mode === 'list_pets') {
        this.surepet.client.getPets().then((pets) => {
            msg.payload = {
                pets: pets
            }
            node.send(msg)
        }).catch(e => {
            node.error(e, msg)
        })
    } else if (config.mode === "get_status") {
        this.surepet.client.getState().then((state) => {
            msg.payload = state;
            node.send(msg);
        }).catch(e => {
            node.error(e, msg)
        })
    }
})

```

---

<div class="post-metadata">

**Author:** ![klose54](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klose54](https://discourse.nodered.org/u/klose54)\
**Post date:** [4 April 2023 15:28 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/14 "2023-04-04T15:28:52Z")

</div>

Thank you for your help @Steve-Mcl

I've made the edit in the surepet.js, It has been well deployed and running for a few minutes now, I will give you some news after some runtime.

EDIT : In fact, I thought I made the changes but I have two surepet.js files in my directory, one in /node\_modules/node-red-contrib-surepet/surepet.js where I made the edit, and an other in /.node-red/node\_modules/node-red-contrib-surepet/surepet.js where when I try to do the changes you gave me, nodered cant restart and it gaves me this :

```auto
 [info] Waiting for missing types to be registered:
 [info] - surepet-credentials
 [info] - surepet

```

---

<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:** [4 April 2023 16:56 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/15 "2023-04-04T16:56:50Z")

</div>

> [@klose54](#):
>
> nodered cant restart and it gaves me this :
> 
> ```auto
> [info] Waiting for missing types to be registered:
> [info] - surepet-credentials
> [info] - surepet
> 
> ```

It is likely you have a syntax error.

Open the file in a proper editor (eg VSCode) & it should reveal any issues.

---

<div class="post-metadata">

**Author:** ![klose54](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klose54](https://discourse.nodered.org/u/klose54)\
**Post date:** [4 April 2023 17:56 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/16 "2023-04-04T17:56:21Z")

</div>

So the entire code with your modifications is

```auto
module.exports = function(RED){
    "use strict"
    var SurePet = require('sure-pet-care-client');

    function SurePetNode(config){
        RED.nodes.createNode(this, config);
        this.surepet = RED.nodes.getNode(config.surepet);
        var node = this;
        //console.log(config);
        node.on('input', function(msg){

            if(config.mode === 'list_pets'){
            this.surepet.client.getPets().then((pets) =>{
                msg.payload = {
                    pets: pets
                }
                node.send(msg)
            }).catch(e => {
                node.error(e, msg)
            })
        } else if(config.mode === "get_status"){
            this.surepet.client.getState().then((state) => {
                msg.payload = state;
                node.send(msg);
            }).catch(e => {
                node.error(e, msg)
            })

        }
            
            /*
            msg.payload = {
                surepet: this.surepet,
                pets: 
            }
            node.send(msg);*/
        });
    }
    RED.nodes.registerType("surepet", SurePetNode);

    function SurePetCredentials(n){
        RED.nodes.createNode(this, n);
        this.username = n.username || this.credentials.username;
        this.password = n.password || this.credentials.password;
        this.client = new SurePet.SurePetCareClient();
        if (this.username && this.password){
            this.log("We have a username and password, attempting to authenticiate.")
            try{
                await this.client.authenticate(this.username, this.password);
                this.status({fill:"green",shape:"dot",text:"Connected."});
            } catch (err){
                this.status({fill:"red",shape:"ring",text:err});
            }
           
        }
    }
    RED.nodes.registerType("surepet-credentials",SurePetCredentials,{
        credentials: {
            username: {type:"text"},
            password: {type:"password"}
        }
    });
}

```

After testing on VSCode, it stated that

`'await' expressions are only allowed within async functions and at the top levels of modules.`

So the soft added a async at

```auto
async function SurePetCredentials(n){

```

With that, VSCode stated no problem detected. But I still have a trouble on deploy of nodered that indicate the same error waiting for missing types, with this line showing during the deploy

`[node-red-contrib-surepet/surepet] TypeError: Cannot convert undefined or null to object (line:55)`

Any idea ?

---

<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:** [4 April 2023 18:57 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/17 "2023-04-04T18:57:58Z")

</div>

> [@klose54](#):
>
> 'await' expressions are only allowed within async functions and at the top levels of modules

Ah ok, try changing this function...

```auto
function SurePetCredentials(n){
        RED.nodes.createNode(this, n);
        this.username = n.username || this.credentials.username;
        this.password = n.password || this.credentials.password;
        this.client = new SurePet.SurePetCareClient();
        const node = this
        if (this.username && this.password){
            this.log("We have a username and password, attempting to authenticate.")
            
            this.client.authenticate(this.username, this.password)
           .then(() => node.status({fill:"green",shape:"dot",text:"Connected.")
           .catch(err => node.status({fill:"red",shape:"ring",text:err}))
        }
    }

```

_written on mobile, untested_

---

<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:** [4 April 2023 19:10 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/18 "2023-04-04T19:10:52Z")

</div>

> [@klose54](#):
>
> With that, VSCode stated no problem detected. But I still have a trouble on deploy of nodered that indicate the same error waiting for missing types, with this line showing during the deploy

Sorry I missed that you added async - that may work instead of changing to promise format in my above post.

However, did you restart node-red after saving changes?

---

<div class="post-metadata">

**Author:** ![klose54](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klose54](https://discourse.nodered.org/u/klose54)\
**Post date:** [4 April 2023 20:16 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/19 "2023-04-04T20:16:49Z")

</div>

I use the command

> sudo systemctl restart nodered.service

After making any changes

---

<div class="post-metadata">

**Author:** ![klose54](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klose54](https://discourse.nodered.org/u/klose54)\
**Post date:** [6 April 2023 17:14 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/20 "2023-04-06T17:14:28Z")

</div>

I tried to add something like this after testing it on VSCode

```auto
            if (typeof this.client.authenticate() !== 'undefined' && this.client.authenticate() !== null) 
                {
                }
            else  
            {
            this.log('⛔️ Object null ');
            }

```

But I'm getting

`[error] Error: Authentication failed: received 422 Unprocessable Entity`

I really don't have any skills in JS so I'm not sure this was the good way to add something that can handle a null object.

---

<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:** [6 April 2023 19:34 UTC](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956/21 "2023-04-06T19:34:10Z")

</div>

client.authenticate is an async function (I pointed that out several posts above). You need to use await or use promise then/catch as I demonstrated in my previous post.

[Next page](https://discourse.nodered.org/t/nodered-keeps-restarting-with-processticksandrejections-error/76956.md?page=2)
