Reputation: 21493
I am using file system with nodejs in windows
to write process logs. I have follwing code
var fs = require('fs');
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
var statusLogStream = fs.createWriteStream("../logs/load stat"+(new Date())+".log");
It turns out with an error
{ [Error: ENOENT: no such file or directory, open 'C:\proc\logs\load stat Mon Apr 18 2016 19:09:32 GMT+0530 (India Standard Time).log']
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\proc\\logs\\load stat Mon Apr 18 2016 19:09:32 GMT+0530 (India Standard Time).log' }
events.js:141
throw er; // Unhandled 'error' event
^
I try the folder to open the file manually C:\\proc\\logs
it doesn't work and C:/proc/logs
this when I replace double backward slash by forward slash I can manually open the folder from explorer.
How to make it working
Why is it taking the double backward slash
instead of forward slash
IMP: The above code works perfectly fine in linux ubuntu
server but not in windows
Upvotes: 5
Views: 15341
Reputation: 619
I had the same issue.
In my project when I ran an express app I noticed that the current working directory was the root directory of the project (while I was trying to read a file that was located in the script's directory).
It could not run the file since process.cwd() !== __dirname
.
You can check it out and console log process.cwd()
in the script you are trying to read the json file.
I just changed the the path to:
const fs = require('fs');
fs.readFileSync(`${__dirname}\\FILENAME`);
Upvotes: 0
Reputation: 4250
The problem is not about the slashes but on how the date is converted to string.
I bet this will work:
var statusLogStream = fs.createWriteStream("../logs/load stat.log");
Update
Windows is complaining about the two colons in the string representation of date (Mon Apr 18 2016 19**:**09**:**32 GMT+0530 (India Standard Time)
)
This could be a good alternative:
var myDate = new Date().toJSON().replace(new RegExp(':', 'g'),'.');
// myDate is now "2016-04-18T15.19.21.174Z"
var statusLogStream = fs.createWriteStream("../logs/load stat"+(myDate)+".log");
Upvotes: 7