Node.js Google Drive Client – Error downloading file: response.data.on is not a function

I am using the Node.js Google Drive client trying to download certain files from a gdrive. When using the example provided in their GitHub I get a Uncaught (in promise) TypeError: res.data.on is not a function error. The file is still created locally, but it’s just an empty file from createWriteStream().

When I log the res variable I get: ReadableStream {locked: false}.

I’m pretty new to streams so this is quite a bit over my head.

Here is my code. You’ll notice it’s almost exactly what the example they provide looks like.

syncFileFromDrive(fileId, filePath) {
        filePath.replace(userDataPath, '');
        filePath = `${userDataPath}/${filePath}`;
        filePath.replaceAll('//', '/');

        logger.info(`Sync file from drive: Syncing file to path: ${filePath}`);
        logger.info(`Sync file from drive: File id: ${fileId}`)

        const dest = fs.createWriteStream(filePath);
        let progress = 0;

        this.drive.files.get({fileId, alt: 'media'}, {responseType: 'stream'}).then(res => {
            console.log(res)
            console.log(res.data)
            res.data
                .on('end', () => {
                    console.log('Done downloading file.');
                    folderStructure.buildFileMenu()
                    resolve(dest)
                })
                .on('error', err => {
                    console.error('Error downloading file.');
                    reject(err);
                })
                .on('data', d => {
                    progress += d.length;
                    if (process.stdout.isTTY) {
                        process.stdout.clearLine();
                        process.stdout.cursorTo(0);
                        process.stdout.write(`Downloaded ${progress} bytes`);
                    }
                })
                .pipe(dest);
            });
    }

96 thoughts on “Node.js Google Drive Client – Error downloading file: response.data.on is not a function”

Leave a Comment