JavaScript import error语言 - Uncaught SyntaxError:不能在模块外使用impor



你好,我的代码在控制台中工作,但它通过html给出了一个导入错误。我搜索了这个问题,但没有找到。

下面是我的代码和错误:

Uncaught SyntaxError:不能在模块外使用import语句

输入图片描述

cloudinary.js

import { Cloudinary } from "@cloudinary/url-gen";
import { fill } from "@cloudinary/url-gen/actions/resize";
const cld = new Cloudinary({
cloud: {
cloudName: 'demo'
}
});
const myImage = cld.image('docs/models'); 
myImage.resize(fill().width(250).height(250));
console.log(myImage.toURL());

cloudinary.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<script src="cloudinary.js"></script>
</body>
</html>

package.json

{
"dependencies": {
"@cloudinary/url-gen": "^1.8.0"
},
"name": "cloudinary",
"description": "",
"version": "1.0.0",
"main": "cloudinary.js",
"devDependencies": {},
"type": "module",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

您正在尝试使用node .js风格的模块解析导入模块。

虽然浏览器支持模块,但它们不支持node .js风格的模块解析。

你需要使用Webpack、Parcel或Rollup这样的工具,以适合浏览器的格式来捆绑你的模块。

或者,你可以寻找直接在web浏览器中使用的模块版本,而不需要任何类型的工具链。

最新更新