[我收到此错误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);
}
///////////////////////////////////////////////////////////////////
您的代码与使用AMD-异步模块定义的RequireJS不兼容。
AMD格式如下:
define(['@google-cloud/storage'], (storage) {
// body of your module here
});
问题是'@google-cloud/storage'
是否与AMD兼容。
这里最简单的解决方案是使用更现代的工具,例如webpack,或者如果仅支持Chrome浏览器,则仅使用本机ES6模块