Node.js app not reading .env file/variables inside linux aws ec2 instance

Viewed 217

I'm new to AWS and I've started and configured an EC2 instance with linux. I made a service worker to auto start the program and told it where the .env file is. Below is how I made the service worker file

sudo vim /etc/systemd/system/NodeServer.service

I then pasted this inside that newly created file

[Unit]
Description=My Node Server
After=multi-user.target

[Service]
ExecStart=/usr/bin/node /home/ec2-user/earmark-backend/dist/api/server.js
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=earmark-backend-server
User=ec2-user
EnvironmentFile=/home/ec2-user/earmark-backend/.env

[Install]
WantedBy=multi-user.target

To create the .env file I ran this command sudo vim /home/ec2-user/earmark-backend/.env

inside the .env file I pasted this code

PLAID_CLIENT_ID=<env_var>

PLAID_SECRET=<env_var>

PLAID_ENV=sandbox

PLAID_PRODUCTS=auth,transactions

PLAID_COUNTRY_CODES=US,CA

PLAID_REDIRECT_URI=<env_var>

PORT=8080

URL=<env_var>

Then when I start the service worker via sudo systemctl start NodeServer.service and make a request I get the error "TypeError [ERR_HTTP_INVALID_HEADER_VALUE]: Invalid value \"undefined\" for header \"PLAID-CLIENT-ID\"\n

However if I start my server with the command PLAID_CLIENT_ID=<env_var> PLAID_SECRET=<env_var> PLAID_ENV=sandbox ...(rest of env vars) node ./dist/api/server.js it works and can read the env variables.

Anyone have any idea why it's not able to read my .env file?

Also, side question: I'd prefer to have my service worker ExecStart command run a bash script, but I'm not exactly sure how I'd setup the script. I have a file start.sh and inside I have

#!/usr/bin/env bash
cd /home/ec2-user/earmark-backend
npm i
npm run clean
npm run build
npm run start

It's not working though, how would I properly format my .sh file to run those commands on start?

0 Answers
Related