7

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

enter image description here

./node_modules/google-spreadsheet/node_modules/google-auth-library/build/src/auth/googleauth.js:17:0 Module not found: Can't resolve 'child_process'

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);
    }
};
juliomalves
  • 42,130
  • 20
  • 150
  • 146
Dieudonne Awa
  • 73
  • 1
  • 1
  • 5
  • Does this help answer the question: [Module not found: Error can't resolve 'child_process', how to fix?](https://stackoverflow.com/questions/54459442/module-not-found-error-cant-resolve-child-process-how-to-fix)? – juliomalves Mar 20 '21 at 14:53
  • it doesn't solve the problem – Dieudonne Awa Mar 21 '21 at 19:46

6 Answers6

9

I just solve it.

Please create next.config.js file in your root. And fill it below.

module.exports = {
  webpack: config => {
    config.node = {
      fs: 'empty',
      child_process: 'empty',
      net: 'empty',
      dns: 'empty',
      tls: 'empty',
    };
    return config;
  },
};

Hoorai!

4

I was having this problem with nextjs 12. Here's what fixed it for me:

My code:

const doc = new GoogleSpreadsheet(SPREADSHEET_ID);
await doc.useServiceAccountAuth({
  client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
  private_key: process.env.GOOGLE_PRIVATE_KEY,
});
    
await doc.loadInfo(); 
console.log('title', doc.title);

My next.config.js:

const nextConfig = {
  reactStrictMode: true,
  webpack: (config, { isServer }) => {
    if (!isServer) {
      config.resolve.fallback.fs = false
      config.resolve.fallback.tls = false
      config.resolve.fallback.net = false
      config.resolve.fallback.child_process = false
    }

    return config
  },      
}

module.exports = nextConfig;

Took inspiration/fix from here

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
camposer
  • 5,152
  • 2
  • 17
  • 15
1

Found this answer due to a similar issue. I later learned for next.js, with some of these api libraries, you must call call this type of code (serverside) in two contexts getStaticProps or getServerSideProps. See this and this for more details.

petrosmm
  • 528
  • 9
  • 23
0

Try changing the import statement to:

const { GoogleSpreadsheet } = require('google-spreadsheet');

Source: https://www.npmjs.com/package/google-spreadsheet

iansedano
  • 6,169
  • 2
  • 12
  • 24
0

The reason is that the library you require uses some nodejs native modules, like path, fs or child_process. As part of the build process nextjs will create js bundles for your client and server separately. The issue is that your client build cannot resolve those nodejs modules. As a workaround you can tell nextjs to ignore these modules for the client build only.

next.config.js

const nextConfig = {
  webpack: (config, { isServer }) => {
    if (!isServer) {
      config.resolve.fallback = {
        fs: false,
        path: false,

      }
    }
    return config
  }
}

module.exports = nextConfig;
Laszlo
  • 2,225
  • 19
  • 22
-1

the library does not support ES6 feature yet

if you look to the module export you will find somthing like this :

module.exports = {
  GoogleSpreadsheet,
  GoogleSpreadsheetWorksheet,
  GoogleSpreadsheetRow,

  GoogleSpreadsheetFormulaError,
};

https://github.com/theoephraim/node-google-spreadsheet/blob/master/index.js

change the import statement to commonjs modules like this :

const { GoogleSpreadsheet } = require('google-spreadsheet');


Belhadjer Samir
  • 1,461
  • 7
  • 15