Error: Module name "@google-cloud/vision" has not been loaded yet for context: _. Use require([])

Error: Module name "@google-cloud/vision" has not been loaded yet for context: _. Use require([])

我收到此错误 Module name "@google-cloud/vision" has not been loaded yet for context: _. Use require([]),当我 运行 我的项目时,我在我的项目中包含了 require.js 并且还在我的 html 文件中包含了脚本标签 <script data-main = "./app.js" src = "./libs/require.js"></script> ,我浏览了很多关于 require.js 的文章,但不明白它的实际用途是什么以及如何解决这个错误。

我也在 Whosebug 上浏览过这个线程,但无法理解 Dynamic require in RequireJS, getting "Module name has not been loaded yet for context" error?

这是我的代码

///////////////////////////////////////////////////////////////
//UPLOAD IMAGE to cloud bucket
function showResults(){
const bucketName = //name-of-bucket;
 const filename = './img2.jpg';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function uploadFile() {
  // Uploads a local file to the bucket
  await storage.bucket(bucketName).upload(filename, {
    // Support for HTTP requests made with `Accept-Encoding: gzip`
    gzip: true,
    // By setting the option `destination`, you can change the name of the
    // object you are uploading to a bucket.
    metadata: {
      // Enable long-lived HTTP caching headers
      // Use only if the contents of the file will never change
      // (If the contents will change, use cacheControl: 'no-cache')
      cacheControl: 'public, max-age=31536000',
    },
  });

  console.log(`${filename} uploaded to ${bucketName}.`);
}

uploadFile().catch(console.error);
}
///////////////////////////////////////////////////////////////////

您的代码与使用 AMD - 异步模块定义的 RequireJS 不兼容。

A​​MD 格式如下所示:

define(['@google-cloud/storage'], (storage) {
  // body of your module here
});

问题是 '@google-cloud/storage' 是否兼容 AMD。

这里最简单的解决方案是使用更现代的工具,例如 webpack,或者如果您仅支持 Chrome 浏览器,则只使用原生 ES6 模块