skip to Main Content

I had the following error and thought I’d share my solution as I couldn’t find it online.

My initial code:

import * as google from 'googleapis';
const sheetsAPI = google.sheets({
    version: 'v4',
    auth: apiKey
});

Gave me the error:
google.sheets is not a function

My solution was rather using the following:

const sheetsAPI = new google.sheets_v4.Sheets({
    auth: apiKey
});

This ran correctly as the available guides said the code in the question would.

I’m going to accept my own answer once available, but would love to hear why I could be getting this problem in the first place if the docs for such a big platform told me to use the initial code?

2

Answers


  1. Chosen as BEST ANSWER

    My solution was rather using the following:

    const sheetsAPI = new google.sheets_v4.Sheets({
        auth: apiKey
    });
    

    This ran correctly as the available guides said the code in the question would.


  2. You need to install and import googleapis

    To install:

    npm install googleapis
    # or
    yarn add googleapis
    

    Then import it as :

    const {google} = require('googleapis');
    

    or

    import { google } from 'googleapis';
    

    If you are using ECMAScript modules.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search