Eureka Moment Wiki

One eureka moment at the time

User Tools

Site Tools


development:js:node_js_service

Node.js daemon/service

Windows
npm install winser
npm install -g pkg
pkg index.js --output myApp.exe
# Use npm_modules/winser/bin/nssm64.exe to create service

Make a myapp.service file (replacing 'myapp' with your app's name, obviously):

Linux
[Unit]
Description=My app
After=network.target
 
[Service]
ExecStart=/var/www/myapp/app.js
Restart=always
User=nobody
# Debian/Ubuntu uses 'nogroup'
# RHEL/Fedora uses 'nobody'
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/myapp
 
[Install]
WantedBy=multi-user.target

Note: /var/www/myapp/app.js should have #!/usr/bin/env node on the very first line

Copy your service file into the /etc/systemd/system

Start it with systemctl start myapp

Enable it on boot with systemctl enable myapp

See logs with journalctl -u myapp

development/js/node_js_service.txt · Last modified: 2018/09/21 10:25 by tplecko