最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is fs.createWriteStream throwing a "Error: ENOENT: no such file or directory" error? - Stack

programmeradmin6浏览0评论

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.

  1. 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 like var fileName = test, var type = pdf, and var newType = "application/pdf".
  2. 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 the WriteStream as defined by dest object.
  3. 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 the createWriteStream.
  4. 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.

  1. 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 like var fileName = test, var type = pdf, and var newType = "application/pdf".
  2. 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 the WriteStream as defined by dest object.
  3. 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 the createWriteStream.
  4. 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!

Share Improve this question edited Mar 4 at 3:10 President James K. Polk 42k28 gold badges109 silver badges145 bronze badges asked Feb 24, 2021 at 1:20 Matt.GMatt.G 2013 silver badges9 bronze badges 6
  • 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 this console.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
 |  Show 1 more ment

3 Answers 3

Reset to default 2

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论