在Astro项目中使用google-apis时出现fs问题

问题描述 投票:0回答:1

在我的 astro 项目中,我正在尝试构建一个 api 端点,以便向 google Sheets api 发出 POST 请求。为了实现这一目标,我安装了 google-apis 包。

问题是在初始化localhost时,打印出如下错误:

  astro  v2.10.14 started in 257ms

  ┃ Local    http://localhost:3000/
  ┃ Network  use --host to expose

✘ [ERROR] Failed to resolve entry for package "fs". The package may have incorrect main/module/exports specified in its package.json. [plugin vite:dep-pre-bundle]

    node_modules/.pnpm/[email protected]/node_modules/google-auth-library/build/src/auth/identitypoolclient.js:18:19:
      18 │ const fs = require("fs");
         ╵                    ~~~~

  This error came from the "onResolve" callback registered here:

    node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1292:20:
      1292 │       let promise = setup({
           ╵                     ^

    at setup (file:///Users/jcbraz/Projects/hugo-araujo-website/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-df561101.js:39921:19)
    at handlePlugins (/Users/jcbraz/Projects/hugo-araujo-website/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:1292:21)
    at buildOrContextImpl (/Users/jcbraz/Projects/hugo-araujo-website/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:978:5)
    at Object.buildOrContext (/Users/jcbraz/Projects/hugo-araujo-website/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:786:5)
    at /Users/jcbraz/Projects/hugo-araujo-website/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:2186:68
    at new Promise (<anonymous>)
    at Object.context (/Users/jcbraz/Projects/hugo-araujo-website/node_modules/.pnpm/[email protected]/node_modules/esbuild/lib/main.js:2186:27)
    at Object.context (/Users/jcbraz/Projects/hugo-araujo-website/node_modules/.pnpm/esbu[email protected]/node_modules/esbuild/lib/main.js:2026:58)
    at prepareEsbuildOptimizerRun (file:///Users/jcbraz/Projects/hugo-araujo-website/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-df561101.js:45967:35)

✘ [ERROR] Failed to resolve entry for package "fs". The package may have incorrect main/module/exports specified in its package.json. [plugin vite:dep-pre-bundle]

    node_modules/.pnpm/[email protected]/node_modules/google-auth-library/build/src/auth/googleauth.js:18:19:
      18 │ const fs = require("fs");

...

经过一番研究,我尝试更改 astro.config.mjs 文件中的 Vite 部分,如以下问题帖子中所述:https://github.com/nuxt/vite/issues/160

import { defineConfig } from "astro/config";
import react from "@astrojs/react";
import { loadEnv } from "vite";
import rollupPluginNodeBuiltins from 'rollup-plugin-node-builtins';
const { GOOGLE_SHEETS_API, SHEET_ID } = loadEnv(process.env.NODE_ENV, process.cwd(), "");

import tailwind from "@astrojs/tailwind";

// https://astro.build/config
export default defineConfig({
  integrations: [
    react(),
    tailwind({
      applyBaseStyles: false,
    }),
  ],
  vite: {
    resolve: {
      alias: {
        fs: rollupPluginNodeBuiltins
      }
    }
  }
});

进行以下更改后,在启动本地主机时仍然出现以下错误:


  🚀  astro  v2.10.14 started in 298ms

  ┃ Local    http://localhost:3000/
  ┃ Network  use --host to expose

✘ [ERROR] Could not read from file: /Users/jcbraz/Projects/hugo-araujo-website/[object Object]

    node_modules/.pnpm/[email protected]/node_modules/google-auth-library/build/src/auth/identitypoolclient.js:18:19:
      18 │ const fs = require("fs");
         ╵                    ~~~~
google-sheets google-apis-explorer astro node.js-fs astrojs
1个回答
0
投票

您不能直接在 astro 文件中使用节点库

如果您仅使用 fs 进行读取操作 您可以使用 Astro.glob()

代替“fs”

如果你需要编写,因为 astro 是一个静态站点生成器,你需要切换到 ssr 模式并使用 astrojs/node 适配器

© www.soinside.com 2019 - 2024. All rights reserved.