Module not found: Can't resolve 'child_process' - google-spreadsheet
Asked Answered
M

7

10

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);
    }
};
Matteo answered 20/3, 2021 at 10:12 Comment(2)
Does this help answer the question: Module not found: Error can't resolve 'child_process', how to fix??Benny
it doesn't solve the problemMatteo
A
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!

Ambur answered 10/4, 2021 at 11:22 Comment(3)
Thanks for your help. I was able to fix it.Matteo
@DieudonneAwa accept the answer if it solved your problemMother
Outdated! - this webpack config is not supportedKlingel
M
12

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

Markhor answered 20/3, 2022 at 17:15 Comment(0)
A
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!

Ambur answered 10/4, 2021 at 11:22 Comment(3)
Thanks for your help. I was able to fix it.Matteo
@DieudonneAwa accept the answer if it solved your problemMother
Outdated! - this webpack config is not supportedKlingel
H
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.

Hass answered 28/6, 2022 at 19:0 Comment(0)
S
0

Try changing the import statement to:

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

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

Sealer answered 23/3, 2021 at 8:36 Comment(0)
M
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;
Mutiny answered 29/4, 2022 at 14:52 Comment(0)
P
0

@Camposer's solution, guided me. In your nextconfig.js file, use

  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;
Pulitzer answered 13/3, 2024 at 17:25 Comment(0)
S
-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');


Stint answered 23/3, 2021 at 8:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.