简体   繁体   中英

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

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..

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

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM