【问题标题】:Module not found: Can't resolve 'fs' on @google-cloud/storage未找到模块:无法解析 @google-cloud/storage 上的“fs”
【发布时间】:2021-01-08 14:58:17
【问题描述】:

尝试列出 GCP 存储中的存储桶时出现 Module not found: Can't resolve 'fs' 错误。

import { Storage } from '@google-cloud/storage';

const googleCloud = new Storage({
  keyFilename: '../../my-project-c1a44bf80be3.json',
  projectId: 'my-project',
});

googleCloud.getBuckets().then((x: any) => console.log(x));

my-project-c1a44bf80be3.json(从 GCP 下载)存在且是项目级别

错误:

event - compiled successfully
event - build page: /
wait  - compiling...
error - ./node_modules/@google-cloud/storage/build/src/file.js:24:0
Module not found: Can't resolve 'fs'
null
Could not find files for / in .next/build-manifest.json
event - compiled successfully

使用googleapis 时会出现同样的错误。 但是,它不是./node_modules/@google-cloud/storage/build/src/file.js:24:0,而是在googleapis 附带的google-auth-library 模块中。

使用yarn 添加。

【问题讨论】:

    标签: node.js npm google-cloud-platform google-cloud-storage google-apis-explorer


    【解决方案1】:

    张贴在 cmets 中提供的 solution 以提高知名度。

    在你的 webpack 配置中,你可以指明某些模块 像 fs 应该被剔除;在这一点上你应该能够 在客户端运行这个库。例如:

    node: {
            // Some libraries import Node modules but don't use them in the browser.
            // Tell Webpack to provide empty mocks for them so importing them works.
            ...config.node,
            fs: 'empty',
            child_process : 'empty',
            net : 'empty',
            tls: 'empty',
          }
    

    【讨论】:

      【解决方案2】:

      您所描述的场景现在由 Google 处理。虽然没有预计到达时间,但该问题正在被优先考虑。

      【讨论】:

      • 是否有更多关于这方面的文档或者我可以在哪里跟进进度?
      • 我可以在此处找到此链接:github.com/googleapis/google-api-nodejs-client/issues/2211 以及您可能需要的一些信息。尽管您使用的是 YARN,但请查看 googleapis/google-auth-library-nodejs#150(评论)“基本上,在您的 webpack 配置中,您可以指出某些模块(如 fs)应该被存根;此时您应该能够在客户端运行这个库”
      猜你喜欢
      • 2022-08-05
      • 2017-04-18
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 2018-06-29
      相关资源
      最近更新 更多