1

I'm attempting to create a script to access data from a Google Sheet using a service account for authentication. However, when I run the script, I encounter an error that I'm having trouble understanding: Error: Cannot find module './key.json'

This situation seems unusual to me because I'm confident that the service account JSON file's path is correct. My file hierarchy is as follows: File Hierarchy

const { google } = require("googleapis");
const { authenticate } = require("@google-cloud/local-auth");

const spreadsheetId = "1ZxZ0kghl5GueyQgTVMILyfsHVFgXPMZT6_qlt1oPBZA";
const range = "Sheet1!A1:D10";

// Create a Google Sheets client
const sheets = google.sheets({
  version: "v4",
});

// Authenticate the client
authenticate({
  keyfilePath: "./key.json",
  scopes: ["https://www.googleapis.com/auth/spreadsheets"],
}).then((credentials) => {
  sheets.auth = credentials;

  // Get the data from the Google Sheets doc
  sheets.spreadsheets.values
    .get({
      spreadsheetId,
      range,
    })
    .then((response) => {
      const data = response.data.values;

      // Print the data
      console.log(data);
    });
});

I attempted to re-download the Service Account JSON file in case it was corrupted, but the error persists.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.