raylewallen
raylewallen

Reputation: 21

Running npm start from ExecStart in systemctl service file

I have the following service file:

[Unit]
Description=MyApp
After=syslog.target network.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=1
WorkingDirectory=/opt/nodejs-sites/MyApp
ExecStart=/usr/bin/npm start
Environment=NODE_ENV=development
User=root
Group=root
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=MyApp

[Install]
WantedBy=multi-user.target

Here is the error from /var/log/syslog

Oct 14 13:00:55 devu18 systemd[1]: Started myapp.
Oct 14 13:00:55 devu18 systemd[3203]: myapp.service: Changing to the requested working directory failed: No such file or directory
Oct 14 13:00:55 devu18 systemd[3203]: myapp.service: Failed at step CHDIR spawning /usr/bin/npm: No such file or directory
Oct 14 13:00:55 devu18 systemd[1]: myapp.service: Main process exited, code=exited, status=200/CHDIR
Oct 14 13:00:55 devu18 systemd[1]: myapp.service: Failed with result 'exit-code'.

I for the life of me can't figure out why it's complaining of cannot find the file. npm start from the same working directory works just fine, no problems. Am I missing some permissions or +x or something somewhere?

Upvotes: 2

Views: 17173

Answers (2)

Jeroen Hartgers
Jeroen Hartgers

Reputation: 21

/bin/npm --prefix $PATH start

This worked for me

Upvotes: 2

Phoenix Himself
Phoenix Himself

Reputation: 1246

This error might happen due to the fact that the executable was run without environment.

Here is a quick fix recipe

You can fix that by creating a Bash Script to do everything you need.

script.sh

#! /bin/bash
source ${HOME}/.bashrc

cd /absolute/path/to/my/project
export NODE_ENV=development
npm start

Now change it's mode to be executable

chmod +x script.sh

Now we can create our service (located for instance in /etc/systemd/system/my-project.service)

my-project.service

[Unit]
Description=My Project
After=syslog.target network.target

[Service]
Type=simple
Restart=always
RestartSec=1
ExecStart=/path/to/my/script.sh
User=root

[Install]
WantedBy=multi-user.target

And now let's run it and enable it

systemctl start my-project
systemctl enable my-project

Troubleshooting (Ubuntu 18.04 LTS)

This happened to me lately that for some reason systemctl couldn't effectively source .bashrc leaving me with an error npm error does not exists This is a workaround that helped me out.

Change the script.sh file's content into the following

#! /bin/bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

cd /absolute/path/to/my/project
export NODE_ENV=development
npm start

Now your npm service should hopefully work.


This solution was tested on:

  • Ubuntu 20.04 LTS, with Node version 14.16.0 installed via NVM (Node Version Manager)
  • Ubuntu 18.04 LTS, with Node version 12.22.6 installed via NVM (Node Version Manager)
  • CentOS 7, with Node version 12.22.6 installed via NVM (Node Version Manager)

Upvotes: 15

Related Questions