next.js 未找到模块:无法解析'fs'(从googleapis导入google时)

xfb7svmp  于 2023-04-30  发布在  Go
关注(0)|答案(1)|浏览(180)

当我尝试从googleapis导入时,我得到了Module not found: Can't resolve 'fs'错误。

//submitData.js
import { google } from "googleapis";
import keys from "../secrets.json";

export default async function submitData(data) {
  try {
    // prepare auth
    const auth = new google.auth.GoogleAuth({
      credentials: {
        client_email: keys.client_email,
        private_key: keys.private_key?.replace(/\\n/g, "\n"),
      },
      scopes: [
        "https://www.googleapis.com/auth/drive",
        "https://www.googleapis.com/auth/drive.file",
        "https://www.googleapis.com/auth/spreadsheets",
      ],
    });

    const sheets = google.sheets({ auth, version: "v4" });

    const sheetName = "DATA";

    const response = await sheets.spreadsheets.values.append({
      spreadsheetId: process.env.SHEET_ID,
      range: `${sheetName}!A3:E3`,
      valueInputOption: "USER_ENTERED",
      requestBody: {
        values: [data.name, data.date],
      },
    });

    return { status: 200, data: response.data };
  } catch (e) {
    return { status: 500, message: e.message ?? "Something went wrong" };
  }
}

这里我想导入submitData。js和传递一些数据

//index.js
import submitData from "../../api/submitData";
...
const example = async (e) => {
    e.preventDefault();
    const data = {
      name: nameDisplay,
      date: day,
    };
    try {
      const response = await submitData(data);
      console.log(response);
      alert(response.data.tableRange);
    } catch (e) {
      console.error(e);
    }
...

错误:

Module not found: Can't resolve 'fs'

Import trace for requested module:
./node_modules/gcp-metadata/build/src/index.js
./node_modules/google-auth-library/build/src/auth/googleauth.js
./node_modules/google-auth-library/build/src/index.js
./node_modules/googleapis/build/src/index.js
./api/submitData.js
./pages/dashboard/index.js

当我浏览互联网时,我找到了这些解决方案,然而它并没有解决我自己的问题。

//next.config.js
/** @type {import('next').NextConfig} */

const nextConfig = {
  reactStrictMode: true,
};

module.exports = nextConfig;

// 1
// module.exports = {
//   webpack5: true,
//   webpack: (config) => {
//     config.resolve.fallback = {
//       fs: false,
//     };

//     return config;
//   },
// };

// 2
// module.exports = {
//   webpack: (config, { isServer }) => {
//     if (!isServer) {
//       config.resolve.fallback.fs = false;
//     }

//     return config;
//   },
// };

注解部分是我尝试的解决方案,另一个错误:

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

Import trace for requested module:
./node_modules/google-auth-library/build/src/index.js
./node_modules/googleapis/build/src/index.js
./api/submitData.js
./pages/dashboard/index.js
ax6ht2ek

ax6ht2ek1#

我得到了这样的回答:https://stackoverflow.com/a/69377128/14974752和您提供的信息。这个解决方案为我工作(在下一个。config.js):

webpack: (config) => {
    config.resolve.fallback = {
      fs: false,
      child_process: false,
      net: false,
      tls: false,
    };

    return config;
  },

相关问题