Sveltekit和Google-Atuth-auth-library Crash

发布于 2025-01-29 08:00:43 字数 1711 浏览 2 评论 0 原文

对于Nodejs来说,我希望在项目中使用Google-Auth-library,因为我需要调用云运行应用程序。我正在使用Sveltekit有几页,所有页面都很好。 我已经安装了Google-Auth-auth-library,并在我想称为云运行的页面中添加了这一行崩溃了。

import {GoogleAuth} from 'google-auth-library';

我遇到的错误:

500

process is not defined

loadProxy@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:1312:23
node_modules/gcp-metadata/node_modules/gaxios/build/src/gaxios.js@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:1318:5
__require@http://localhost:3000/node_modules/.vite/deps/chunk-OO5LXEG7.js?v=de891e38:26:50
node_modules/gcp-metadata/node_modules/gaxios/build/src/index.js@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:1521:20
__require@http://localhost:3000/node_modules/.vite/deps/chunk-OO5LXEG7.js?v=de891e38:26:50
node_modules/gcp-metadata/build/src/index.js@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:3386:20
__require@http://localhost:3000/node_modules/.vite/deps/chunk-OO5LXEG7.js?v=de891e38:26:50
node_modules/google-auth-library/build/src/auth/googleauth.js@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:25077:23
__require@http://localhost:3000/node_modules/.vite/deps/chunk-OO5LXEG7.js?v=de891e38:26:50
node_modules/google-auth-library/build/src/index.js@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:25757:24
__require@http://localhost:3000/node_modules/.vite/deps/chunk-OO5LXEG7.js?v=de891e38:26:50
@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:25838:35

到目前为止,我还没有找到解决此问题的方法。关于这种行为的可能原因以及如何解决的任何想法?

Being new to NodeJS, I'm looking to use the google-auth-library in my project as I need to call a Cloud Run app. I'm using SvelteKit have a couple of pages and all works fine.
I've installed the google-auth-library and adding this single line in the page that I want to call the Cloud Run crashed the app.

import {GoogleAuth} from 'google-auth-library';

The error I get:

500

process is not defined

loadProxy@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:1312:23
node_modules/gcp-metadata/node_modules/gaxios/build/src/gaxios.js@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:1318:5
__require@http://localhost:3000/node_modules/.vite/deps/chunk-OO5LXEG7.js?v=de891e38:26:50
node_modules/gcp-metadata/node_modules/gaxios/build/src/index.js@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:1521:20
__require@http://localhost:3000/node_modules/.vite/deps/chunk-OO5LXEG7.js?v=de891e38:26:50
node_modules/gcp-metadata/build/src/index.js@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:3386:20
__require@http://localhost:3000/node_modules/.vite/deps/chunk-OO5LXEG7.js?v=de891e38:26:50
node_modules/google-auth-library/build/src/auth/googleauth.js@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:25077:23
__require@http://localhost:3000/node_modules/.vite/deps/chunk-OO5LXEG7.js?v=de891e38:26:50
node_modules/google-auth-library/build/src/index.js@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:25757:24
__require@http://localhost:3000/node_modules/.vite/deps/chunk-OO5LXEG7.js?v=de891e38:26:50
@http://localhost:3000/node_modules/.vite/deps/google-auth-library.js?v=de891e38:25838:35

So far, I have not found a way to fix this. Any ideas on the possible cause for this behaviour and how I can fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒人说梦 2025-02-05 08:00:43

Google-Auth-library 是一个node.js库。它取决于节点特定的API,因此无法在浏览器上运行。

Sveltekit提供的几个功能是服务器端渲染,客户端路由和水合。这意味着,默认情况下,Sveltekit将在服务器上运行您的代码(为了在第一次加载您的站点时为完整的HTML文件提供完整的HTML文件)和客户端(以提供交互性和活泼的导航)。

由于 Google-Auth-auth-library 不支持浏览器,因此在客户端导入时会收到该错误。

有两种处理此问题的主要方法:

  • 在端点和钩子中使用服务器端特定的库

    与组件不同, endpoints and and page endpoints 使远离<

  • 应使用浏览器检查在服务器中运行的后卫代码

    如果您需要组件中的某些代码或加载才能在服务器或浏览器中运行,则可以使用浏览器环境变量:

      import {browser}从'$ app/env`;
    
    如果(浏览器){
        somebrowseronlyCode();
    }
     

    这很有用,例如,通过省略SSR期间但增加客户端的组件来逐步增强组件。

    不幸的是,由于ESM的性质,您不能有条件地使用常规导入。在这些情况下,您可以使用动态导入来确保仅在特定环境中加载库:

      if(!browser){
        导入('Some-Server-library')。然后(lib =&gt; lib.dostuff());
    }
     
    重要

    !!浏览器的组件中的代码仅在服务器端渲染期间运行。虽然它保证它是正确的环境,但也意味着在客户端导航期间不会再次执行它 - 它只能影响初始服务的HTML。

    另一个问题是,您必须小心不要在这些支票之外参考保护资源,否则您可能会意外地泄漏服务器和客户端之间的事情。

就您而言,第一个选择可能是正确的解决方案。 Google-Auth-auth-library 在浏览器中没有位置,您当然想在每次需要时执行代码,而不仅仅是在SSR期间。

google-auth-library is a Node.js-only library. It depends on Node-specific APIs and thus it can't run on the browser.

A couple of features provided by SvelteKit are server-side rendering and client-side routing and hydration. This means that, by default, SvelteKit will run your code both on the server (in order to serve a complete HTML file the first time users load your site) and on the client (in order to provide interactivity and snappy navigation).

Since google-auth-library doesn't support the browser, you get that error when it's imported in the client.

There are two main approaches to deal with this:

  • Use server-side-specific libraries in endpoints and hooks

    Unlike components, endpoints and hooks run exclusively in the server, so you're free to use server-side dependencies there. Page endpoints make it especially convenient to migrate away from load in these cases, but otherwise standalone endpoints are a fetch() away.

  • Guard code that should run in the server with browser checks

    If you need some code inside a component or in load to run exclusively in the server or the browser, you can use the browser environment variable:

    import { browser } from `$app/env`;
    
    if (browser) {
        someBrowserOnlyCode();
    }
    

    This is useful, for example, for progressively enhancing components by omitting things during SSR but augmenting the component on the client.

    Unfortunately, due to the nature of ESM you can't use regular imports conditionally. In these cases, you can use dynamic imports to ensure a library is only loaded in a specific environment:

    if (!browser) {
        import('some-server-library').then(lib => lib.doStuff());
    }
    
    Important

    Code in components guarded by !browser will only run during server-side rendering. While it guarantees it's the right environment, it also means the it will not be executed again during client-side navigation — it can only affect the initial served HTML.

    Another issue is that you have to be careful not to reference guarded resources outside these checks, or else you could accidentally leak things between server and client.

In your case, it's likely that the first option would be the right solution. google-auth-library has no place in the browser, and you'd certainly want to execute your code every time it's needed, not only during SSR.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文