Diagnose Node.js startup failures with service logs

Understand where Node.js service logs come from, inspect them with journalctl and tail, and troubleshoot startup failures.

🎯 Introduction

When Node.js fails to start, systemctl status helps determine whether the service is running, but may not explain why it failed. Invalid port settings or a missing entry file can both prevent startup.

Instead of changing configuration at random, read the logs, find the error, and fix the problem it describes.

This article reuses linux-notes.service to inspect service logs with journalctl and file logs with tail. A simulated failure takes you from identifying the cause to verifying recovery.


🧱 Where logs come from

Logs record what a program does: when it starts, which requests it receives, and which errors occur.

In the earlier Node.js service, console.log() and console.error() write to standard output and standard error, respectively. These are output channels, not necessarily files on disk.

When systemd manages Node.js with typical default logging settings, its logging system, journald, usually collects these streams. View them with journalctl, which also shows systemd's own startup, shutdown, and failure records.

Request the health endpoint, then inspect the latest 20 service log entries:

curl -i http://127.0.0.1:3000/health
sudo journalctl -u linux-notes.service -n 20 --no-pager

The second command's options mean:

  • -u linux-notes.service: Show logs for this service only.
  • -n 20: Show the latest 20 entries.
  • --no-pager: Print directly without opening a pager.

If you are using the earlier example program that logs requests, you should find similar entries:

The following is example output. Timestamps, hostnames, and PIDs vary; these are not results obtained from your server:

Nov 12 14:10:03 ubuntu systemd[1]: Started linux-notes.service - Linux Notes API.
Nov 12 14:10:03 ubuntu node[2481]: Server listening on port 3000
Nov 12 14:10:15 ubuntu node[2481]: GET /health status=200

Read each line in two parts. Nov 12 14:10:15 ubuntu node[2481] gives the timestamp, hostname, program name, and PID. GET /health status=200 is the application's request record. Lines with systemd[1] record systemd service events. Exact messages depend on what the example program prints.

For large logs, narrow the scope, such as entries from the current boot and the last 10 minutes:

sudo journalctl -b -u linux-notes.service --since '10 minutes ago' --no-pager

-b selects the current boot, and --since sets the starting time. A smaller time window reduces irrelevant information.


🛠 Follow service and file logs

To watch new logs while interacting with the service, use journalctl -f. -f waits for and displays new entries.

In the first terminal connected to Ubuntu, run:

sudo journalctl -fu linux-notes.service

Keep it open and send a request from a second terminal:

curl -i http://127.0.0.1:3000/api/notes

Return to the first terminal. If the example program still logs requests as described earlier, you should see a similar entry:

The first terminal may show this new line (example output):

Nov 12 14:12:08 ubuntu node[2481]: GET /api/notes status=200

A new entry at the bottom confirms that follow mode is working; status=200 indicates a successful response. If the program does not log requests, this line will not appear. Check whether the earlier logging code is present.

This makes the sequence visible: a request reaches the program, and a new log appears in the watching terminal. Use the same approach to observe output when a request fails.

Applications can also write logs directly to files. If you created app.log in the earlier file viewing exercise, run:

tail -n 10 -f "$HOME/linux-lab/logs/app.log"

For example, if the file contains these lines, the terminal may initially show:

2026-11-12 14:00:01 app started
2026-11-12 14:00:05 GET /health status=200
2026-11-12 14:00:08 GET /api/notes status=200

When another line is appended, the terminal displays it. This illustrates tail -f; it does not mean the earlier app.log contains these exact entries, or that accessing Node.js endpoints necessarily writes to that file.

tail reads the end of a file:

  • -n 10: Display the last 10 lines initially.
  • -f: Wait for and display appended content.

The distinction: journalctl queries records collected by the logging system, while tail reads a specified file. They do not always show the same logs. Choose according to where the logs actually reside.

Press Control + C in the relevant terminal to stop following logs. This ends only the viewing command, not the Node.js service.


🧰 Diagnose startup failures from logs

Simulate a realistic problem: set an invalid port so Node.js cannot start, then use logs to identify the cause.

1. Introduce an invalid port setting

Open the systemd service configuration created earlier:

sudo vim /etc/systemd/system/linux-notes.service

Find the port environment variable under [Service]:

Environment=PORT=3000

Temporarily change it to:

Environment=PORT=70000

PORT is passed to Node.js as an environment variable. Port numbers generally range from 0 to 65535, with availability also subject to application and system restrictions. 70000 deliberately creates an invalid value.

Save, reload systemd's configuration, and restart the service:

sudo systemctl daemon-reload
sudo systemctl restart linux-notes.service

daemon-reload makes systemd reread modified unit files. Without it, systemd may keep using the old configuration.

2. Inspect the failure state and error logs

Check service status:

systemctl status linux-notes.service --no-pager

The service may show failed or briefly be restarting automatically. The relevant part may look like this (example output):

● linux-notes.service - Linux Notes API
     Loaded: loaded (/etc/systemd/system/linux-notes.service; enabled)
     Active: failed (Result: exit-code) since Thu 2026-11-12 14:15:04 CST
    Process: 2602 ExecStart=/usr/bin/node /home/ubuntu/linux-lab/app.js (code=exited, status=1/FAILURE)
   Main PID: 2602 (code=exited, status=1/FAILURE)

Active: failed indicates failure, and status=1/FAILURE means the startup process exited with a nonzero status. The ExecStart path is illustrative; use the command you actually configured. Status shows that the service is unhealthy; logs provide the cause.

Before filtering keywords, read the latest 30 entries directly:

sudo journalctl -u linux-notes.service -n 30 --no-pager

Look for errors around the failed startup. If the earlier program validates the port range, logs might resemble this example:

Nov 12 14:15:04 ubuntu systemd[1]: Starting linux-notes.service - Linux Notes API...
Nov 12 14:15:04 ubuntu node[2602]: PORT must be an integer from 1 to 65535
Nov 12 14:15:04 ubuntu systemd[1]: linux-notes.service: Main process exited, code=exited, status=1/FAILURE
Nov 12 14:15:04 ubuntu systemd[1]: linux-notes.service: Failed with result 'exit-code'.

In the second line, node[2602] identifies an application message. PORT must be ... is the error to investigate. The next two lines record systemd's outcome: the process exited and the service failed.

The message requires PORT to be an integer between 1 and 65535. Its exact wording comes from the example's validation code, not a fixed Node.js message. Without custom validation, you may instead see Node.js report an invalid port.

Either way, if the logs identify an invalid port, inspect the recently changed Environment=PORT=70000 to locate the cause.

Once you know a useful keyword, filter the logs, for example:

sudo journalctl -u linux-notes.service -n 30 --no-pager | grep -i 'port'

grep -i 'port' matches lines containing port, ignoring case. Read the full error first, then choose search terms from what you find; do not assume every problem uses a fixed keyword.

3. Fix the configuration and verify recovery

Edit the service file again and restore:

Environment=PORT=3000

Reload configuration and restart:

sudo systemctl daemon-reload
sudo systemctl restart linux-notes.service
systemctl status linux-notes.service --no-pager
curl -i http://127.0.0.1:3000/health

Check two results:

  1. systemctl status reports a running service, such as Active: active (running).
  2. /health returns HTTP 200, confirming that requests are actually served.

The health check may produce this example output; the body depends on your endpoint implementation:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{"status":"ok"}

200 OK is the key verification here. Headers and body need not match the example exactly.

If repeated startup failures trigger systemd's start limit, first run:

sudo systemctl reset-failed linux-notes.service
sudo systemctl restart linux-notes.service

reset-failed clears the recorded failure state and associated start rate counters. It is not required after every repair and cannot replace fixing the underlying configuration error.

You have now completed the troubleshooting cycle: detect a problem → inspect logs → identify faulty configuration → fix it → verify recovery.


🧱 Log troubleshooting tips

Do not search only for ERROR. Error formats differ: a busy port may produce EADDRINUSE, while access problems may show Permission denied. Read the complete logs around the failure, then narrow the search based on actual content to avoid missing clues.

File logs also consume disk space over time, so long-running services need retention and cleanup policies. Tools such as logrotate manage ordinary log files; systemd's journal has its own storage and retention mechanisms. Recognizing this is enough here; detailed sizing and rotation policies can be covered in later operations material.


❓ Knowledge check

Q1: Why can journalctl show logs after Node.js failed to start?

systemd records startup and exit events as well as collecting application output. An application may leave errors even if it never serves requests. If it never ran at all, most messages may come from systemd itself.

Q2: Should I use journalctl or tail -f?

Use journalctl when systemd manages Node.js and collects its output. Use tail -f for files written directly by the application. First identify the log destination.


🧪 Exercise

Complete two steps:

  1. Observe normal requests: Follow logs with journalctl -fu linux-notes.service, then request /api/notes and /missing. If the example logs request status codes, find the corresponding 200 and 404 entries.
  2. Troubleshoot independently: Set PORT to 70000 again and inspect status and logs after startup fails. Read the full logs before searching predefined text, identify the port error, restore 3000, and verify a healthy status and a 200 response from /health.

Success criteria: Distinguish application messages from systemd events and use the error to identify which configuration to change, rather than merely memorizing commands.


🎉 You can now inspect Node.js service logs, diagnose startup failures, and verify a successful repair.