I am working on EC2 instance and I have placed my node application project in the /var/www/. I am using upstart script to start the node application. The upstart script is placed in /etc/init/. In the application I am parsing a file as below to access config vars:
var _conf = JSON.parse(fs.readFileSync('./config/config.json'));
When I run the app in foreground with the command node app.js it works fine. It is able to parse the file into JSON. But when I run the upstart script the following error is thrown.
fs.js:549
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open './config/config.json'
at Error (native)
at Object.fs.openSync (fs.js:549:18)
at Object.fs.readFileSync (fs.js:397:15)
at Object.<anonymous> (/var/www/machaao-api/node_modules/machaao-kafka/index.js:3:27)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
[2015-12-09T07:52:05.257Z] (sys) Starting
Below is my upstart script:
#!upstart
description "API Server"
author "Author"
start on startup
stop on shutdown
respawn
respawn limit 5 20
script
echo "script started..." >> /var/log/project.log
echo $$ > /var/run/project-api.pid
exec node /var/www/project-api/app.js >> /var/log/project-api.log 2>&1
end script
pre-start script
echo "Pre-start script started..."
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/project-api.log
end script
pre-stop script
echo "Pre-stop script started..."
rm /var/run/project-api.pid
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/project-api.log
end script
Can someone explain what is the issue? It says no such file or directory when I know it is there. I am a novice linux user. Is this because of some permission issues?