Braz
Braz

Reputation: 153

fs problem when using google-apis in Astro project

In my astro project, I'm trying to build an api end point in order to make a POST request to the google sheets api. To achieve this, I've installed the google-apis package.

The problem in question is when the localhost is initialised, the following error is printed out as follows:

  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/[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");

...

After some research, I've tried to change the Vite section in the astro.config.mjs file as described in the following issue post: 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
      }
    }
  }
});

After the following change, I still got the following error when spinning up the localhost:


  🚀  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");
         ╵                    ~~~~

Upvotes: 0

Views: 1133

Answers (1)

Karthik S Salian
Karthik S Salian

Reputation: 78

You cannot use node libraries directly inside the astro file.

If you are using fs only for read operations, instead of "fs" you can use Astro.glob().

If you need to do write since astro is a static site generator you need to switch to ssr mode and use the astrojs/node adapter.

Upvotes: 3

Related Questions