【问题标题】:i am getting error while importing @google-cloud/storage我在导入 @google-cloud/storage 时遇到错误
【发布时间】:2021-03-24 14:35:29
【问题描述】:

导入@google-cloud/storage 后出现错误

环境详情

  • 操作系统:macOS Big Sur
  • Node.js 版本:v12.18.4
  • npm 版本:6.14.6
  • @google-cloud/存储版本:^ 5.6.0
  • 框架 nuxt js 版本:Nuxt.js @ v2.14.7

复制步骤

  • 安装@google-cloud/storage

    yarn add @google-cloud/storage 或 npm install @google-cloud/storage --save

导入

const {Storage} = require('@google-cloud/storage');
const storage = new Storage({
keyFilename : path.join(__dirname, './file.json'),
projectId: "xxx-xxx-xxx"
});

就在这之后我得到了这个错误

ERROR in ./node_modules/google-auth-library/build/src/auth/googleauth.js Module not found: Error: Can't resolve 'child_process' in '/Users/xxx/xxxx/node_modules/google-auth-library/build/src/auth' ERROR in ./node_modules/@google-cloud/storage/build/src/file.js Module not found: Error: Can't resolve 'fs' in '/Users/xxx/xxxx/node_modules/@google-cloud/storage/build/src' ERROR in ./node_modules/@google-cloud/storage/build/src/bucket.js Module not found: Error: Can't resolve 'fs' in '/Users/xxx/xxxx/node_modules/@google-cloud/storage/build/src' ERROR in ./node_modules/configstore/node_modules/make-dir/index.js Module not found: Error: Can't resolve 'fs' in '/Users/xxx/xxxx/node_modules/configstore/node_modules/make-dir' ERROR in ./node_modules/google-auth-library/build/src/auth/googleauth.js Module not found: Error: Can't resolve 'fs' in '/Users/xxx/xxxx/node_modules/google-auth-library/build/src/auth' ERROR in ./node_modules/google-p12-pem/build/src/index.js Module not found: Error: Can't resolve 'fs' in '/Users/xxx/xxxx/node_modules/google-p12-pem/build/src' ERROR in ./node_modules/graceful-fs/graceful-fs.js Module not found: Error: Can't resolve 'fs' in '/Users/xxx/xxxx/node_modules/graceful-fs' ERROR in ./node_modules/gtoken/build/src/index.js Module not found: Error: Can't resolve 'fs' in '/Users/xxx/xxxx/node_modules/gtoken/build/src' ERROR in ./node_modules/write-file-atomic/index.js Module not found: Error: Can't resolve 'fs' in '/Users/xxx/xxxx/node_modules/write-file-atomic' ERROR in ./node_modules/http-proxy-agent/dist/agent.js Module not found: Error: Can't resolve 'net' in '/Users/xxx/xxxx/node_modules/http-proxy-agent/dist' ERROR in ./node_modules/https-proxy-agent/dist/agent.js Module not found: Error: Can't resolve 'net' in '/Users/xxx/xxxx/node_modules/https-proxy-agent/dist' ERROR in ./node_modules/http-proxy-agent/dist/agent.js Module not found: Error: Can't resolve 'tls' in '/Users/xxx/xxxx/node_modules/http-proxy-agent/dist' ERROR in ./node_modules/https-proxy-agent/dist/agent.js Module not found: Error: Can't resolve 'tls' in '/Users/xxx/xxxx/node_modules/https-proxy-agent/dist'

我试图通过添加覆盖 webpack 配置

 config.node : {
        fs: 'empty', 
        net: 'empty', 
        tls: 'empty', 
        child_process: 'empty', 
        __filename: true, 
        __dirname: true
     }

这是我找到的唯一解决方案,但我仍然遇到错误

TypeError: The "original" argument must be of type Function
    at promisify (util.js?3022:602)
    at eval (index.js?5051:7)
    at Object../node_modules/@google-cloud/storage/node_modules/get-stream/index.js (index.js:275)
    at __webpack_require__ (runtime.js:854)
    at fn (runtime.js:151)
    at Object.eval (file.js?5fe9:20)
    at eval (file.js:3042)
    at Object../node_modules/@google-cloud/storage/build/src/file.js (index.js:167)
    at __webpack_require__ (runtime.js:854)
    at fn (runtime.js:151)

【问题讨论】:

  • 尝试删除 projectId,文档上说当你使用 keyFilename 时不需要 projectId googleapis.dev/nodejs/storage/latest/global.html#StorageOptions
  • @Pablote 我只是通过导入 @google-cloud/storage const {Storage} = require('@google-cloud/storage');
  • 看起来 OP 提交了 open issue 并且其他人也遇到了它。我相信这个问题最好由客户端库的维护者解决

标签: node.js vue.js webpack google-cloud-storage nuxt.js


【解决方案1】:

您在客户端代码上使用@google-cloud/storage 的问题,这个库应该在server side only 上使用。

记住 Nuxt 是一个 SSR(服务器端渲染),即使它在服务器上渲染,很多代码也会在客户端。您可以尝试在服务器端组件上使用它,例如 The serverMiddleware Property

在客户端运行存储尝试使用npm firebase storage

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 2021-07-27
    相关资源
    最近更新 更多