2

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)
      }


}
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449

2 Answers2

0

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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
0

I encountered similar problems, you have to manually configure it to include the credentials.json file in the netlify.toml.

For example, if you wanted your functions to use credentials.json, add this to your netlify.toml file:

[functions]
    included_files = ["credentials.json"] 

You can read more about it here: https://www.netlify.com/blog/2021/08/12/how-to-include-files-in-netlify-serverless-functions/