[英]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. 我是Node JS的新手,请帮助我解决这个问题。 I want to create a text file in node js.
我想在节点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.
但是在docker内部,我得到了。
ERROR Error: ENOENT: no such file or directory, open '../upload/2018-11-07.txt' 错误错误:ENOENT:没有这样的文件或目录,请打开'../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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.