node-google-spreadsheet icon indicating copy to clipboard operation
node-google-spreadsheet copied to clipboard

Module not found: Can't resolve 'child_process'

Open dieudonneAwa opened this issue 3 years ago • 1 comments

I am trying to save form data to a spreadsheet in Next.js but I keep getting this error which appears as soon as I import google-spreadsheet

Error Screenshot 2021-03-20 at 11 03 40

Bellow is what I have that is causing the error.

// The error appears when I do this import
import { GoogleSpreadsheet } from "google-spreadsheet";

const SPREADSHEET_ID = process.env.NEXT_PUBLIC_SPREADSHEET_ID;
const SHEET_ID = process.env.NEXT_PUBLIC_SHEET_ID;
const CLIENT_EMAIL = process.env.NEXT_PUBLIC_GOOGLE_CLIENT_EMAIL;
const PRIVATE_KEY = process.env.NEXT_PUBLIC_GOOGLE_SERVICE_PRIVATE_KEY;

const doc = new GoogleSpreadsheet(SPREADSHEET_ID);

const appendSpreadsheet = async (row) => {
    try {
      await doc.useServiceAccountAuth({
        client_email: CLIENT_EMAIL,
        private_key: PRIVATE_KEY,
      });
      // loads document properties and worksheets
      await doc.loadInfo();

      const sheet = doc.sheetsById[SHEET_ID];
      const result = await sheet.addRow(row);
      return result;
    } catch (e) {
      console.error("Error: ", e);
    }
  };

Help will be highly appreciated.

dieudonneAwa avatar Mar 20 '21 10:03 dieudonneAwa

Try this :)

https://github.com/theoephraim/node-google-spreadsheet/issues/374#issuecomment-761547527

C-E-Rios avatar May 06 '21 19:05 C-E-Rios

It would appear that NextJS does not support running google-spreadsheet in the client. as long as you only try to run these methods server-side you should be good.

I just had the same error, and switching my code to server side fixed the problem.

AarooneyFarr avatar Mar 01 '23 20:03 AarooneyFarr

google-auth-library is now an optional peer dependency and you can pass in a raw token. Please give it a try and reopen if you run into any problems.

theoephraim avatar Jun 26 '23 17:06 theoephraim