SecondLemon
SecondLemon

Reputation: 1003

nodejs fs(filesystem) working locally but not on server

I have my script all ready for my server except a variable is not loading on the server because it seems the file system doesn't interpret the path in the same way on the server (Ubuntu 14.04 x64 vmlinuz-3.13.0-57-generic). When I test my server locally on OS X 10.11.1 it all works fine but on the server the file cannot be found.

var jsonHourlyFile = './data/jsonHourly.json';
console.log(jsonHourlyFile);
console.log(fs.existsSync(jsonHourlyFile));
if (fs.existsSync(jsonHourlyFile)) { //Why was this in the function in the first place...?
  console.log("loaded JsonHourlyFile")
  var jsonHourly = jsonfile.readFileSync(jsonHourlyFile);
} else {
  var jsonHourly = JSON;
  console.log("jsonHourlyFile not found!")
}

This Returns true on my computer and false on the server. I've copied all the file correctly to the server using FileZilla and can confirm they are there.

I suspect it has to do with the file path. Though I thought that using ./ will always point to the right map. Who could elaborate on this?

Upvotes: 1

Views: 1055

Answers (1)

John Pink
John Pink

Reputation: 637

I must comment on your last sentence :

"Though I thought that using ./ will always point to the right map."

This is not true. What ./ means, is "in this directory", i.e. where the script is executed.

On you local machine, you say your script is executed in /Users/me/WebstormProjects/WebTest, which means that your JSON file should be located in /Users/me/WebstormProjects/WebTest/data/. We know this is true since the script works on your machine.

Now, on the server, you say the script is executed in /root/WebTest/bin. So unless there is a data/ folder in that particular directory, which contains your JSON file (who's path would therefore be /root/WebTest/bin/data/jsonHourly.json), your program will not execute as expected.

There are many ways to solve this, I couldn't tell which one is the best. You just need to make sure that your local machine and the server have the correct paths to the file, which can be very different considering both Operating Systems aren't the same.

More about paths on Wikipedia.

Upvotes: 2

Related Questions