javascript - ERROR Error: ENOENT: no such file or directory, open when create a text file in node js - Stack Overflow

I'm new to node js please help me to solve this. I want to create a text file in node js. I'

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
Add a ment  | 

2 Answers 2

Reset to default 3

Thanks 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() + ...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744882494a4598920.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信