Diagathe Josué
Diagathe Josué

Reputation: 11986

Google Drive API : ENOENT - but my file is just in the same directory

ENOENT - but my file is just in the same directory ... Hi all I try to sent a file, birds.mp3, to my Google Drive using API. The file got to be reached by a function to be sent.

Despite that the file I try to send is just in the same folder that the code concerned, my console return me :

events.js:183

throw er; // Unhandled 'error' event

^

Error: ENOENT: no such file or directory, open './birds.mp3'

Here my tree :

-- folder
  |-- birds.mp3
  |-- quickstart.js

Here my quickstart.js

module.exports.insertDrive = function (req) {
  console.log("callback reached.")
var url = req.body.url;
var folderId = 'id';
var fileMetadata = {
  'name': req.body.word,
  parents:  "id"
};
var media = {
  mimeType: 'audio/*',
  body: fs.createReadStream("./birds.mp3") // PATH FUNCTION HERE
};
drive.files.create({
  resource: fileMetadata,
  media: media,
  fields: 'id'
}, function (err, file) {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    console.log('File Id: ', file.id);
  } 
})};

I can't figure out why my file can't be reached. I have try several tricks like path.resolve and all, I have try to push my birds.mp3 in several folder if any, but that have failed.

Thanks.

Upvotes: 0

Views: 353

Answers (1)

jfriend00
jfriend00

Reputation: 707466

If you're trying to load a file from the same directory as the current module and pass that to the Google API, then use __dirname instead of ./. The ./ uses the current working directory which will depend upon how the overall program was invoked and will not point at your module directory.

So, if the intended file is in the same directory as your module, then change this:

fs.createReadStream("./birds.mp3")

to this:

fs.createReadStream(path.join(__dirname, "birds.mp3"));

Upvotes: 1

Related Questions