reactjs 未找到模块:无法解析"child_process"-谷歌电子表格

deyfvvtc  于 2023-01-08  发布在  React
关注(0)|答案(6)|浏览(170)

我尝试将表单数据保存到Next.js中的电子表格中,但每次导入google-spreadsheet时都会出现此错误
错误

./node_modules/谷歌电子表格/node_modules/谷歌认证库/build/src/认证/谷歌认证. js:17:0模块未找到:无法解析'child_process'
贝娄是我所拥有的导致错误的东西。

// 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);
    }
};
6yoyoihd

6yoyoihd1#

我只是解决它。
请在根目录下创建next.config.js文件,并在下面填写。

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

万岁!

3zwjbxry

3zwjbxry2#

我在使用nextjs 12时遇到了这个问题。以下是为我修复的方法:
我的代码:

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

我的下一个. 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
  },
  future: {
    webpack5: true,
  },
  fallback: {
    fs: false,
    tls: false,
    net: false,
    child_process: false
  },
}

module.exports = nextConfig;

从这里获得灵感/修正

lhcgjxsq

lhcgjxsq3#

由于类似的问题,我发现了这个答案。我后来学习了next.js,对于其中的一些API库,你必须在两个上下文getStaticPropsgetServerSideProps中调用这种类型的代码(服务器端)。更多细节请参见this和this

2ekbmq32

2ekbmq324#

尝试将import语句更改为:

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

来源:https://www.npmjs.com/package/google-spreadsheet

pkmbmrz7

pkmbmrz75#

原因是您需要的库使用了一些nodejs本地模块,如pathfschild_process。作为构建过程的一部分,nextjs将分别为您的客户端和服务器创建js bundle。问题是您的客户端构建无法解析这些nodejs模块。作为一种解决方案,您可以让nextjs仅在客户端构建时忽略这些模块。

    • 下一个配置js**
const nextConfig = {
  webpack: (config, { isServer }) => {
    if (!isServer) {
      config.resolve.fallback = {
        fs: false,
        path: false,

      }
    }
    return config
  }
}

module.exports = nextConfig;
qv7cva1a

qv7cva1a6#

存储库尚不支持ES6功能
如果你查看模块导出,你会发现类似这样的东西:

module.exports = {
  GoogleSpreadsheet,
  GoogleSpreadsheetWorksheet,
  GoogleSpreadsheetRow,

  GoogleSpreadsheetFormulaError,
};

https://github.com/theoephraim/node-google-spreadsheet/blob/master/index.js
将import语句更改为commonjs模块,如下所示:

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

相关问题