I'm new to node js please help me to solve this. I want to create a text file in node js. I'm using following code segment for that.
var d = new Date();
var filename = '../upload/' + d.getFullYear() + '-' + pad((d.getMonth() + 1).toString()) + '-' + pad((d.getDate()).toString()) + '.txt';
if (currentlogstreamfilename != filename) {
currentlogstreamfilename = filename;
console.log("Path: " + currentlogstreamfilename);
currentlogstream = fs.createWriteStream(currentlogstreamfilename, { flags: 'a' });
}
locally this is working perfectly. But inside the docker I'm getting.
ERROR Error: ENOENT: no such file or directory, open '../upload/2018-11-07.txt'
Any solution for this..
I'm new to node js please help me to solve this. I want to create a text file in node js. I'm using following code segment for that.
var d = new Date();
var filename = '../upload/' + d.getFullYear() + '-' + pad((d.getMonth() + 1).toString()) + '-' + pad((d.getDate()).toString()) + '.txt';
if (currentlogstreamfilename != filename) {
currentlogstreamfilename = filename;
console.log("Path: " + currentlogstreamfilename);
currentlogstream = fs.createWriteStream(currentlogstreamfilename, { flags: 'a' });
}
locally this is working perfectly. But inside the docker I'm getting.
ERROR Error: ENOENT: no such file or directory, open '../upload/2018-11-07.txt'
Any solution for this..
Share asked Nov 7, 2018 at 7:46 AmilaAmila 521 gold badge1 silver badge11 bronze badges 3- what kind of a setup you have in your Dockerfile ? – dilantha111 Commented Nov 7, 2018 at 7:50
- I'm using Operating System: Ubuntu 18.04.1 LTS OSType: linux Architecture: x86_64 – Amila Commented Nov 7, 2018 at 7:57
-
FROM alpine:3.7 # Update RUN apk add --update nodejs # Bundle app source COPY . /app # Install app dependencies RUN cd /app; npm install EXPOSE 3005 CMD ["node", "/app/app.js"]
– Amila Commented Nov 7, 2018 at 8:02
2 Answers
Reset to default 3Thanks for all. I used node path as following. It worked fine.
var path = require('path');
var filename = path.join(__dirname,'../upload/' + d.getFullYear() + '-' + pad((d.getMonth() + 1).toString()) + '-' + pad((d.getDate()).toString()) + '.txt');
don't use relative path name such ../
. use an absolute path in your application. you can say
filename = __dirname + '/upload + d.getYear() + ...
or if you want to save this file in the parent directory you should first get running process root path. which you can say:
filename = process.cwd() + 'uploads/' + d.getYear() + ...