admin管理员组文章数量:1320787
Here is the code:
const dest = fs.createWriteStream('./src/Pages/downloads/' + fileName + '.' + type)
await drive.files.export(
{fileId: fileId, mimeType: newType},
{responseType: 'stream'},
function (err, response) {
if (err)
return console.log(err);
response.data.on('error', err => {
console.log("Found at 911 " + err);
})
.pipe(dest, function () {
console.log('file path written.')
})
.on('end', () => {
console.log("sent file.")
})
});
A few things to explain.
- This is within a POST request that takes several variables from a Google Drive file, such as its name, id, and mime-type. These are the "fileName" and "type" variables that show up within the
WriteStream
request. They are already predefined and not the problem. So for the sake of this question, assume that the variables are something likevar fileName = test
,var type = pdf
, andvar newType = "application/pdf"
. - The
drive.files.export
function is an asynchronous call to the Google Drive API that finds the file/folder/item with the specific id noted in fileId and exports it. What I am trying to achieve here is exporting the file that fits that specific id into theWriteStream
as defined bydest
object. - The
./src/Pages/downloads/
path does exist. Obviously the particular file (for the test case let's say test.pdf) doesn't yet, but that's why I am using thecreateWriteStream
. - I have already tried changing the path from
./src/Pages/downloads/
to__dirname + "/src/Pages/downloads"
and the same error below occurs.
With all of this said, I get an error that looks like this.
Error: ENOENT: no such file or directory, open './src/Pages/downloads/test.pdf Emitted 'error' event on WriteStream instance at: at internal/fs/streams.js:375:14
This error occurs at the const dest = fs.createWriteStream(...)
event. Would anyone be able to explain what is going on? Would really appreciate it. Thank you!
Here is the code:
const dest = fs.createWriteStream('./src/Pages/downloads/' + fileName + '.' + type)
await drive.files.export(
{fileId: fileId, mimeType: newType},
{responseType: 'stream'},
function (err, response) {
if (err)
return console.log(err);
response.data.on('error', err => {
console.log("Found at 911 " + err);
})
.pipe(dest, function () {
console.log('file path written.')
})
.on('end', () => {
console.log("sent file.")
})
});
A few things to explain.
- This is within a POST request that takes several variables from a Google Drive file, such as its name, id, and mime-type. These are the "fileName" and "type" variables that show up within the
WriteStream
request. They are already predefined and not the problem. So for the sake of this question, assume that the variables are something likevar fileName = test
,var type = pdf
, andvar newType = "application/pdf"
. - The
drive.files.export
function is an asynchronous call to the Google Drive API that finds the file/folder/item with the specific id noted in fileId and exports it. What I am trying to achieve here is exporting the file that fits that specific id into theWriteStream
as defined bydest
object. - The
./src/Pages/downloads/
path does exist. Obviously the particular file (for the test case let's say test.pdf) doesn't yet, but that's why I am using thecreateWriteStream
. - I have already tried changing the path from
./src/Pages/downloads/
to__dirname + "/src/Pages/downloads"
and the same error below occurs.
With all of this said, I get an error that looks like this.
Error: ENOENT: no such file or directory, open './src/Pages/downloads/test.pdf Emitted 'error' event on WriteStream instance at: at internal/fs/streams.js:375:14
This error occurs at the const dest = fs.createWriteStream(...)
event. Would anyone be able to explain what is going on? Would really appreciate it. Thank you!
-
You might see the directory when you look in your file browser, but
fs
does not see the directory from its perspective. change the path to./
and see if you still get the error. – Randy Casburn Commented Feb 24, 2021 at 1:26 - It's already like that in the WriteStream. @RandyCasburn You do make a good point though. Is there anything else I can do? – Matt.G Commented Feb 24, 2021 at 1:28
-
1
No, I meant pletely remove every part of the current file path and replace it with only
./
. You might get surprised at where it drops the file :-) – Randy Casburn Commented Feb 24, 2021 at 1:29 -
1
It seems likely that
'./src/Pages/downloads/'
is not pointing where you expect it to be. I'd suggest logging thisconsole.log(path.normalize('./src/Pages/downloads/' + fileName + '.' + type))
and see exactly where it points. It's probably pointing to a directory that doesn't exist. – jfriend00 Commented Feb 24, 2021 at 1:47 - have you tried using path.resolve() ? – Matt Brandt Commented Feb 24, 2021 at 1:49
3 Answers
Reset to default 2In my case, finally I found it's because the filename is not correct, it contains :
.
My system is windows.
In my case, the path was relative (i.e. "./tmp_folder/1.txt"
) instead of a full path (i.e. "/Users/name/project_name/tmp_folder/1.txt"
).
This:
fs.createWriteStream(path.join('./tmp_folder', fileName), { encoding: 'utf8' });
Turned to this (__dirname
is the difference):
fs.createWriteStream(path.join(__dirname, './tmp_folder', fileName), { encoding: 'utf8' });
In my case, a write stream could not be created without first making the directory. Using fs.mkDir()
or fs.mkdirSync()
to create the directory in advance fixes the issue.
本文标签:
版权声明:本文标题:javascript - Why is fs.createWriteStream throwing a "Error: ENOENT: no such file or directory" error? - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742082057a2419758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论