【问题标题】:Error: Module name "@google-cloud/vision" has not been loaded yet for context: _. Use require([])错误:尚未为上下文加载模块名称“@google-cloud/vision”:_。使用要求([])
【发布时间】:2020-06-03 05:25:26
【问题描述】:

我收到此错误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 的文章,但无法'不明白它的实际用途是什么以及如何解决这个错误。

我也在 StackOverFlow 上浏览过这个帖子,但无法理解 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);
}
///////////////////////////////////////////////////////////////////

【问题讨论】:

    标签: javascript html google-chrome requirejs


    【解决方案1】:

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

    AMD 格式如下所示:

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

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

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

    【讨论】:

      猜你喜欢
      • 2022-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 2016-11-16
      相关资源
      最近更新 更多