node.js writeFile 没有这样的文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34811222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
writeFile no such file or directory
提问by Karl Morrison
I have a file(data.filean image), I would like to save this image. Now an image with the same name couldexist before it. I would like to overwrite if so or create it if it does not exist since before. I read that the flag "w" should do this.
我有一个文件(data.file一张图片),我想保存这张图片。现在,在它之前可能存在具有相同名称的图像。如果是这样,我想覆盖它,或者如果它从以前就不存在就创建它。我读到标志“w”应该这样做。
Code:
代码:
fs.writeFile('/avatar/myFile.png', data.file, {
flag: "w"
}, function(err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
Error:
错误:
[Error: ENOENT: no such file or directory, open '/avatar/myFile.png'] errno: -2, code: 'ENOENT', syscall: 'open', path: '/avatar/myFile.png'
[Error: ENOENT: no such file or directory, open '/avatar/myFile.png'] errno: -2, code: 'ENOENT', syscall: 'open', path: '/avatar/myFile.png'
回答by SergeS
This is probably because you are trying to write to root of file system instead of your app directory '/avatar/myFile.png'-> __dirname + '/avatar/myFile.png'should do the trick, also check if folder exists. node.js won't create parent folder for you.
这可能是因为您正在尝试写入文件系统的根目录而不是应用程序目录'/avatar/myFile.png'->__dirname + '/avatar/myFile.png'应该可以解决问题,还要检查文件夹是否存在。node.js 不会为您创建父文件夹。
回答by Lukas Liesis
Many of us are getting this error because parent pathdoes not exist. E.g. you have /tmpdirectory available but there is no folder "foo" and you are writing to /tmp/foo/bar.txt.
我们中的许多人都收到此错误,因为父路径不存在。例如,您有/tmp可用的目录,但没有文件夹“foo”并且您正在写入/tmp/foo/bar.txt.
To solve this, you can use mkdirp- adapted from How to write file if parent folder doesn't exist?
要解决此问题,您可以使用mkdirp- 改编自How to write file if parent folder does not exist?
Option A) Using Callbacks
选项 A) 使用回调
const mkdirp = require('mkdirp');
const fs = require('fs');
const getDirName = require('path').dirname;
function writeFile(path, contents, cb) {
mkdirp(getDirName(path), function (err) {
if (err) return cb(err);
fs.writeFile(path, contents, cb);
});
}
Option B) Using Async/Await
选项 B) 使用异步/等待
Or if you have an environment where you can use async/await:
或者,如果您有一个可以使用 async/await 的环境:
const mkdirp = require('mkdirp');
const fs = require('fs');
const writeFile = async (path, content) => {
await mkdirp(path);
fs.writeFileSync(path, content);
}
回答by Svetlin Totev
I solved a similar problem where I was trying to create a file with a name that contained characters that are not allowed. Watch out for that as well because it gives the same error message.
我解决了一个类似的问题,我试图创建一个名称包含不允许的字符的文件。也要注意这一点,因为它给出了相同的错误消息。
回答by Masihur
Actually, the error message for the file names that are not allowed in Linux/ Unix system comes up with the same error which is extremely confusing. Please check the file name if it has any of the reserved characters. These are the reserved /, >, <, |, :, &characters for Linux / Unix system. For a good read follow this link.
实际上,Linux/Unix 系统中不允许的文件名的错误消息也出现了相同的错误,非常令人困惑。请检查文件名是否有任何保留字符。这些是Linux / Unix 系统的保留/、>、<、|、:、&字符。要阅读此链接,请点击此处。
回答by Stev
I ran into this error when creating some nested folders asynchronously right before creating the files. The destination folders wouldn't always be created before promises to write the files started. I solved this by using mkdirSyncinstead of 'mkdir' in order to create the folders synchronously.
在创建文件之前异步创建一些嵌套文件夹时,我遇到了这个错误。在承诺开始写入文件之前,并不总是会创建目标文件夹。我通过使用mkdirSync而不是 'mkdir' 来同步创建文件夹来解决这个问题。
try {
fs.mkdirSync(DestinationFolder, { recursive: true } );
} catch (e) {
console.log('Cannot create folder ', e);
}
fs.writeFile(path.join(DestinationFolder, fileName), 'File Content Here', (err) => {
if (err) throw err;
});
回答by markemus
Ihad this error because I tried to run:
我有这个错误,因为我试图运行:
fs.writeFile(file)
fs.unlink(file)
...lots of code... probably not async issue...
fs.writeFile(file)
in the same script. The exception occurred on the second writeFilecall. Removing the first two calls solved the problem.
在同一个脚本中。第二次writeFile调用时发生异常。删除前两个调用解决了问题。
回答by ofir_aghai
you can use './' as a prefix for your path.
您可以使用“./”作为路径的前缀。
in your example, you will write:
在你的例子中,你会写:
fs.writeFile('./avatar/myFile.png', data.file, (err) => {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
回答by ktretyak
In my case, I use async fs.mkdir()and then, without waiting for this task to complete, I tried to create a file fs.writeFile()...
就我而言,我使用 asyncfs.mkdir()然后,没有等待此任务完成,我尝试创建一个文件fs.writeFile()...

