Skip to content

Instantly share code, notes, and snippets.

Last active June 10, 2024 14:18
Show Gist options
  • Save joepie91/73ce30dd258296bd24af23e9c5f761aa to your computer and use it in GitHub Desktop.
Save joepie91/73ce30dd258296bd24af23e9c5f761aa to your computer and use it in GitHub Desktop.
Running a Node.js application using nvm as a systemd service

Read this first!

Hi there! Since this post was originally written, nvm has gained some new tools, and some people have suggested alternative (and potentially better) approaches for modern systems. Make sure to have a look at the comments to this article, before following this guide!

The original article

Trickier than it seems.

1. Set up nvm

Let's assume that you've already created an unprivileged user named myapp. You should never run your Node.js applications as root!

Switch to the myapp user, and do the following:

  1. curl -o- | bash (however, this will immediately run the nvm installer - you probably want to just download the manually, and inspect it before running it)
  2. Install the latest stable Node.js version: nvm install stable

2. Prepare your application

Your package.json must specify a start script, that describes what to execute for your application. For example:

"scripts": {
    "start": "node app.js"

3. Service file

Save this as /etc/systemd/system/my-application.service:

Description=My Application



You'll want to change the User, Description and ExecStart/WorkingDirectory paths to reflect your application setup.

4. Startup script

Next, save this as /home/myapp/ (adjusting the username in both the path and the script if necessary):

. /home/myapp/.nvm/
npm start

This script is necessary, because we can't load nvm via the service file directly.

Make sure to make it executable:

chmod +x /home/myapp/

5. Enable and start your service

Replace my-application with whatever you've named your service file after, running the following as root:

  1. systemctl enable my-application
  2. systemctl start my-application

To verify whether your application started successfully (don't forget to npm install your dependencies!), run:

systemctl status my-application

... which will show you the last few lines of its output, whether it's currently running, and any errors that might have occurred.


Copy link

@bryanlarsen this is awesome, works perfectly, even with screen:


ExecStart=/usr/bin/screen -dmL LOGFILE.log -S SCREEN-NAME /home/pi/.nvm/nvm-exec npm start --prefix /PATH-TO-YOUR-PROJECT


Copy link

Vindexus commented Mar 3, 2019

What does ". /home/myapp/.nvm/" do?

Copy link

joepie91 commented Jul 3, 2019

@Vindexus It executes the code in /home/myapp/.nvm/ in the current context of the shell, without spawning a new one. If you were to do bash /home/myapp/.nvm/, it would instantiate a new shell and run it in there.

Copy link

Denkneb commented Aug 22, 2019

KillMode=process looks like it will only kill the start script and not any of the PIDs started by it. This should probably be omitted, or set to KillMode=control-group if you are finding systemctl stop my.service isn't being stopped properly.


Copy link

donnlee commented Aug 28, 2022

Truly great recipe.
I removed the KillMode=process line (as mentioned above) and added line RestartSec=30

Copy link

kyeno commented Jan 2, 2023

Problem is this whole snippet will not allow you to run systemctl stop my.service

Copy link

kyeno commented Jan 2, 2023

This is what works for both starting and stopping.

Environment=NODE_VERSION=17.3.0 NODE_ENV=production
ExecStart=/root/.nvm/nvm-exec /root/project/index.js

Copy link

nvm-exec is the right answer, thanks to point it out

Copy link

crazynewidea commented Feb 8, 2024

For the nvm-exec method, in my case the Environment=NODE_VERSION... arguments did not work, and I received the following error:
No NODE_VERSION provided; no .nvmrc file found
I had to include a .nvmrc file in the project WorkingDirectory. The .nvmrc file is a text file that simply contains the version number that you want to run as such as "10.20.0".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment