Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: browser support #337

Closed
jasonswearingen opened this issue Sep 13, 2020 · 9 comments
Closed

Feature: browser support #337

jasonswearingen opened this issue Sep 13, 2020 · 9 comments
Labels
type: docs Improvement to the documentation for an API. web

Comments

@jasonswearingen
Copy link

jasonswearingen commented Sep 13, 2020

Is your feature request related to a problem? Please describe.
Want 'one net io to rule them all' for use in browser and node projects

Describe the solution you'd like
switch gaxios to use isomorphic dependencies. such as cross-fetch instead of node-fetch

Describe alternatives you've considered
use a bloated complex library like axios

Additional context
I see https://github.com/googleapis/gaxios/tree/master/browser-test so I'm confused. If browser is already supported, add some info to the readme!

@JustinBeckwith JustinBeckwith added the type: docs Improvement to the documentation for an API. label Sep 13, 2020
@JustinBeckwith
Copy link
Contributor

So yeah, this totally works in the browser! We don't pre-package a minified/webpacked version in the browser, but if you follow along with our webpack config, you can make it work :)

The cross-fetch thing is funny to me. I think we just swap in regular ol browser fetch if it's available.

@jasonswearingen
Copy link
Author

Thank you, I have verified it works with webpack.

The key is to make sure a user has this in their webpack config:

module: {
    rules: [
      {
        test: /node_modules\/https-proxy-agent\//,
        use: 'null-loader',
      },

With that, it works.

I'll leave the issue open for the docs to get updated. feel free to close at any time!

@lil5
Copy link

lil5 commented Aug 24, 2021

This still depends on the assumption that everyone is using & wants to edit their webpack configuration file.

@edward3h
Copy link

I'm using Vite instead of webpack. Is there an equivalent config setting?

I currently get an error like this when trying to use in browser.

gaxios.ts:66 Uncaught ReferenceError: process is not defined
at loadProxy (gaxios.ts:66:5)
at node_modules/gaxios/build/src/gaxios.js (gaxios.ts:75:1)
at __require (chunk-OO5LXEG7.js?v=d0f656e5:26:50)
at node_modules/gaxios/build/src/index.js (index.ts:15:1)
at __require (chunk-OO5LXEG7.js?v=d0f656e5:26:50)
at node_modules/gcp-metadata/build/src/index.js (index.ts:8:1)
at __require (chunk-OO5LXEG7.js?v=d0f656e5:26:50)
at node_modules/google-auth-library/build/src/auth/googleauth.js (googleauth.js:19:21)
at __require (chunk-OO5LXEG7.js?v=d0f656e5:26:50)
at node_modules/google-auth-library/build/src/index.js (index.js:17:22)

@edward3h
Copy link

I worked around the "process is not defined" error by adding to my vite.config.ts:

    define: {
        'process.env': {}
    }

I'll try and find answers for my next issue before commenting again here :)

FossPrime added a commit to FossPrime/gaxios that referenced this issue Apr 20, 2022
googleapis#337

This improves compatibility with blunders that don't implement hacks like Vite, as well as non Node.js JavaScript environments such as Browsers, Deno, v8js and WebContainers.
bcoe pushed a commit that referenced this issue Apr 20, 2022
#337

This improves compatibility with blunders that don't implement hacks like Vite, as well as non Node.js JavaScript environments such as Browsers, Deno, v8js and WebContainers.
@rattrayalex
Copy link

Visitors to this thread may be interested in https://github.com/developit/redaxios which wraps native fetch() with the axios API.

@bcoe bcoe added the web label Jun 10, 2022
@bcoe
Copy link
Contributor

bcoe commented Jun 10, 2022

Since this issue was opened, we've upgraded to a newer webpack, have integration tests for it, and have an example webpack config in the project root:

https://github.com/googleapis/gaxios/blob/main/webpack.config.js

Please let us know if folks continue bumping into any issues.

@bcoe bcoe closed this as completed Jun 10, 2022
@asontha
Copy link

asontha commented Aug 31, 2022

Added this to my nextjs webpack config like so:

file: next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  webpack: (config, options) => {
    config.module.rules.push({
      test: /node_modules\/https-proxy-agent\//,
      use: 'null-loader',
    });

    return config
  }
}

module.exports = nextConfig

Still getting this error:
image

@mcanaleta
Copy link

I worked around the "process is not defined" error by adding to my vite.config.ts:

    define: {
        'process.env': {}
    }

I'll try and find answers for my next issue before commenting again here :)

Didn't work for me. Instead I what works for me (vite / react) is the vite-plugin-node-polyfills plugin:

import { defineConfig } from "vite";
import { nodePolyfills } from "vite-plugin-node-polyfills";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react(),
    nodePolyfills(), // https://www.npmjs.com/package/vite-plugin-node-polyfills
  ],

//...

  },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: docs Improvement to the documentation for an API. web
Projects
None yet
Development

No branches or pull requests

8 participants