NodeJS React Systemd Service not working

Solution:

Solution 1:

I encountered the same problem after an upgrade, my systemd service started exiting with error code 0 every time it was started.

This is due to a February update to react-scripts start.js

Kolega's answer is really the best approach to resolve this:

Set the CI (Continuous Integration) environment variable to true. This can be done in the service file with:

Environment=CI=true

Here is a sample .service file:

# Sample - My Sample Service file
[Unit]
Description=Sample - Sample Service to start a React server
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/npm start --prefix /home/pi/sample
Environment=CI=true
WorkingDirectory=/home/pi/sample
StandardOutput=inherit
StandardError=inherit
Restart=no
User=pi

[Install]
WantedBy=multi-user.target

Please up-vote Kolega's answer if the above works for you.

My original solution is below, which requires making changes to react distribution files:

To resolve this I commented out the change that makes start.js exit when stdin ends. Since with a service there is no stdin input, it exists before the App starts. This is in node-modules/react-scripts/scripts/start.js

    if (isInteractive || process.env.CI !== 'true') {
      // Gracefully exit when stdin ends
      // process.stdin.on('end', function() {
      //   devServer.close();
      //   process.exit();
      // });
      process.stdin.resume();
    }

The start.js change to exit when stdin ends is documented in https://github.com/facebook/create-react-app/pull/7203/files/108bafe5d5b85c9544dd05b5ed42b4e90c66b2ca

Solution 2:

I solved this by adding the following to the [Service] section of my .service file:

StandardInput=tty-force