NodeJS React Systemd Service not working

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


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

StandardInput=tty-force


If you want to have non-exiting service, with npm start you have to define a CI variable which prevents quitting after reading EOF. This solution can be run in users session as well. The important part from my service file is Environment=CI=True.

[Unit]
Description=Project

[Service]
ExecStart=/usr/bin/npm run start --prefix /home/ondrej/project
Environment=CI=true

[Install]
WantedBy=default.target