# How to run a node js app on my NR box?

**URL:** <https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789>\
**Category:** General\
**Created:** [27 April 2021 05:41 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789 "2021-04-27T05:41:21Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [27 April 2021 05:41 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/1 "2021-04-27T05:41:22Z")

</div>

Guys,

I have just found a Node.js app that is run from the command line as follows

node tuya-mqtt.js

This app lets me control and monitor my new heatpump system using mqtt

I run node red on a VM so have plenty of grunt and memory available - is there an easy way to get this app to run in the background on startup of the machine ?

I know this is not strictly a NR question - but doing a google search has led me down a rabbit warren and i do not want to bugger up the machine running my main NR flow

Craig

---

<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:** [27 April 2021 06:03 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/2 "2021-04-27T06:03:31Z")

</div>

What OS are you running on?  
If it is Debian based such as Ubuntu or Raspbian then a simple systemd script should do it.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [27 April 2021 06:50 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/3 "2021-04-27T06:50:20Z")

</div>

Sorry - should have said.

yep Ubuntu based in a VM - 18LTS

regards

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [27 April 2021 07:21 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/4 "2021-04-27T07:21:08Z")

</div>

OK just going through now and trying to set it up as a service with systemd as the user pi

Working through a few issues now

Craig

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [27 April 2021 08:00 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/5 "2021-04-27T08:00:43Z")

</div>

If it just runs as root the "old fashioned easy way" is just to add it to /etc/rc.local - you would need to fully path both the executable and the script name but is an easy fix.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [27 April 2021 08:21 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/6 "2021-04-27T08:21:06Z")

</div>

yeah seems to run fine as root - just trying to get it to run as a restricted user (pi) and having problems as it does not seem to understand paths to find the config files - just trying to work through permutations on that before i thrown in the towel and just run it as root

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [27 April 2021 08:22 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/7 "2021-04-27T08:22:38Z")

</div>

In fact it must be something i am doing/not understanding.

I can run it from the commandline as the pi user from within its own directory (under the home directory for the pi user) - but when i run it as a service it is having problems - must be something to do with paths to pass into the service - just trying to understand how to do that one

Craig

---

<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:** [27 April 2021 20:53 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/8 "2021-04-27T20:53:07Z")

</div>

When you run from systemd, don't forget that you don't get any of the usual environment variables - like path. However, there are ways to inherit them if you need to. You'll need to look that up I'm afraid, systemd has too many options to just remember 😀

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [27 April 2021 22:35 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/9 "2021-04-27T22:35:19Z")

</div>

Yeah thanks Julian - i think it is the same boat i am in at the moment - just trying to work through the permutations to make that happen !

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [28 April 2021 00:59 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/10 "2021-04-28T00:59:55Z")

</div>

OK for anyone else that wants to use this nodejs app (which produces much cleaner output than the other Tuya apps i could find)

The solution i ended up finding was on the openhab forums

To create it as a service

To run tuya-mqtt as a systemd-service, you can create a file:

```auto
 sudo vi /lib/systemd/system/tuya-mqtt.service

```

and add the following lines:

```auto
 [Unit]
 Description=tuya-mqtt
 After=mosquitto.service

 [Service]
 ExecStart=/usr/bin/node /home/pi/tuya-mqtt/tuya-mqtt.js
 Restart=always
 User=pi
 Group=pi
 Environment=PATH=/usr/bin/
 Environment=NODE_ENV=production
 WorkingDirectory=/home/pi/tuya-mqtt/

 [Install]
 WantedBy=multi-user.target

```

After saving the file, you have to run:

```auto
 sudo systemctl daemon-reload

```

Now you can start the service:

```auto
 sudo systemctl start tuya-mqtt.service

```

Check if the service is runnning properly:

sudo systemctl status tuya-mqtt.service

Post #1 in this link

> **[Tuya devices to Openhab via MQTT - a working solution (without flashing)!](https://community.openhab.org/t/tuya-devices-to-openhab-via-mqtt-a-working-solution-without-flashing/106756)**
>
> Dear OH-community, I found several postings about adding tuya devices to OH. Thanks to @tsightler finally there is a working solution now. Tom rewrote his whole code with some modifications on the underlaying TuyAPI. One of the highlights here is...

Craig

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [28 April 2021 06:40 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/11 "2021-04-28T06:40:12Z")

</div>

Do you really mean hom/pi on the execstart line ?

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [28 April 2021 22:34 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/12 "2021-04-28T22:34:01Z")

</div>

Good Spotting - sorry for the typo - fixed now !

Craig

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 April 2021 10:16 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/13 "2021-04-29T10:16:45Z")

</div>

> [@craigcurtin](#):
>
> `ExecStart=/usr/bin/node /home/pi/tuya-mqtt/tuya-mqtt.js`

Did you mean to include the space?

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [30 April 2021 03:27 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/14 "2021-04-30T03:27:46Z")

</div>

If you mean in between /usr/bin/node and then/home/pi etc etc

Then yes

It is saying to run the Node program and then run the javascript file in the tuya-mqtt directory (or are you saying there is another space somewhere ?

Craig

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [30 April 2021 03:34 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/15 "2021-04-30T03:34:56Z")

</div>

> [@craigcurtin](#):
>
> `sudo systemctl start tuya-mqtt.service`

Don't you need to do `sudo systemctl enable tuya-mqtt.service` also if you want the thing to start at reboot

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [30 April 2021 08:38 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/16 "2021-04-30T08:38:29Z")

</div>

Yep - Exactly - i think that was in the link that i provided also - but good to point out thanks

Craig

---

<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:** [14 May 2021 08:39 UTC](https://discourse.nodered.org/t/how-to-run-a-node-js-app-on-my-nr-box/44789/17 "2021-05-14T08:39:21Z")

</div>

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