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

Viewed 2632

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

0 Answers
Related