Reputation: 1753
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?
Upvotes: 2
Views: 1511
Reputation: 303
I'm not sure at the moment how node resolves such a relative paths so maybe try something like this, before parsing the json
var path = require('path');
console.log(path.resolve('./config/config.json'));
and see if it prints an absolute path you wish it to be.
Upvotes: 0
Reputation: 14590
Probably you need only to cd
into the node directory and start it from there:
exec cd /var/www/project-api && node app.js >> /var/log/project-api.log 2>&1
You could also change the app script to get the absolute path so you don't need to cd into the directory:
var fs = require('fs');
var path = require('path');
var _conf = JSON.parse(fs.readFileSync(path.join(__dirname, 'config/config.json')));
console.log(_conf);
Upvotes: 1