errorMessage: "Error: ENOENT: no such file or directory, open '/var/task/credentials.json'" errorType: "Runtime.UnhandledPromiseRejection"

Viewed 1053

I am working on a Next JS app using google Sheet api for a personal project. Now this works completely fine on my local. But when I am deploying it to Netlify, this error pops up.

Can someone put up a solution? Thanks!

Here is my code:

import { google } from "googleapis";

export default async function form1(req, res){

    if (req.method === 'POST') {
        // Process a POST request
        console.log("POST hit")
        const auth = new google.auth.GoogleAuth({
            keyFile: "credentials.json",
            scopes: "https://www.googleapis.com/auth/spreadsheets",
        })

        //create client instancce
    const client = await auth.getClient();

    //instance of google sheet api

    const googleSheets = google.sheets({version: "v4", auth: client});


    const spreadsheetId = "*********55653"

    //Write rows TO sheet
    const {name, email, phone} = req.body;

    await googleSheets.spreadsheets.values.append({
        auth,
        spreadsheetId,
        range:"Subs!A:B",
        valueInputOption: "USER_ENTERED",
        resource: {
            values: [
                [name, email, phone]
            ],
        }
    })

    res.send("Success")
      } else {
        // Handle any other HTTP method
        res.status(405)
      }


}
1 Answers

The credentials.json is the file which contains the all of the information about your google project. Without it your application can not run.

The error message no such file or directory, open '/var/task/credentials.json'" means that your application cant find the json key file in that directory.

Make sure that this file was uploaded to your server.

Related