CodexBloom - Programming Q&A Platform

Debian 11 - Systemd service not starting due to missing environment variable

👀 Views: 94 💬 Answers: 1 📅 Created: 2025-06-04
linux node.js systemd JavaScript

I need some guidance on I recently switched to I'm following best practices but I'm learning this framework and I've been struggling with this for a few days now and could really use some help..... I'm running a Node.js application on Debian 11 and set up a systemd service to manage it. However, the service fails to start because it want to find an environment variable that I need for the application. I've created the service file at `/etc/systemd/system/my-node-app.service` and defined the environment variable like this: ```ini [Unit] Description=My Node App After=network.target [Service] ExecStart=/usr/bin/node /path/to/app.js Environment=MY_ENV_VAR=/path/to/resource Restart=always User=nodeuser [Install] WantedBy=multi-user.target ``` When I try to start the service with `sudo systemctl start my-node-app`, I get the following behavior in the logs: ``` my-node-app.service: Failed to start My Node App: Environment variable MY_ENV_VAR not found ``` I’ve checked the syntax of the service file and reloaded the systemd daemon with `sudo systemctl daemon-reload`, but the behavior continues. Additionally, when I run the application manually using `MY_ENV_VAR=/path/to/resource node /path/to/app.js`, it works fine. I also verified that the environment variable exists in the shell I’m using. What could be causing systemd to not recognize the environment variable in this context, and how can I resolve this scenario? Any insights would be greatly appreciated! My development environment is Linux. What's the best practice here? For context: I'm using Javascript on macOS. Has anyone dealt with something similar? Any suggestions would be helpful. I'm coming from a different tech stack and learning Javascript. Could this be a known issue?