Dieudonne Awa
Dieudonne Awa

Reputation: 113

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

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);
    }
};

Upvotes: 11

Views: 21009

Answers (7)

Asomba Chinoso
Asomba Chinoso

Reputation: 51

@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;

Upvotes: 1

camposer
camposer

Reputation: 5612

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

Upvotes: 13

petrosmm
petrosmm

Reputation: 550

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.

Upvotes: 2

Laszlo
Laszlo

Reputation: 2328

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;

Upvotes: 2

Jargal Tumurbaatar
Jargal Tumurbaatar

Reputation: 149

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!

Upvotes: 10

Belhadjer Samir
Belhadjer Samir

Reputation: 1659

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');


Upvotes: -1

iansedano
iansedano

Reputation: 6481

Try changing the import statement to:

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

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

Upvotes: 1

Related Questions