Uncaught SyntaxError: Cannot use import statement outside a module的解决方法

本地html文件中的script标签引入ES6的模块,直接在浏览器中打开该html文件,发现报错了:Uncaught SyntaxError: Cannot use import statement outside a module
报错
对应的index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="./indexes.js"></script>
</body>
</html>

对应的indexes.js:

import cal from './calculatores.js'

console.log('sum: ', cal.add(1,2))

对应的calculatores.js:

export default {
    add: function(a, b) {
        return a + b
    }
}

这里报错的原因是用了es6的语法, 浏览器默认将它作为js解析会出现问题,需要将它作为模块导入,script标签默认type="text/javascript",需要改为type="module",更改后的index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="module" src="./indexes.js"></script>
</body>
</html>

在浏览器中打开,发现又报错了:Access to script at 'file:///E:/**********/indexes.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
报错
从错误提示看,脚本是被跨域策略给拦截了,跨域请求只支持这些协议:http, data, chrome, chrome-extension, chrome-untrusted, https.,而我们的协议是file,这里我们需要本地起一个服务器来作为资源的提供方,简单的方式是安装VSCode的一个扩展Live Server
liveserver
该插件安装完成后,在index.html页面右键选择Open with Live Server

在这里插入图片描述
然后就可以成功运行了:
成功运行
url上我们可以看到,Live Server为我们默认启动的服务器地址是127.0.0.1,端口号为5500,这样就解决跨域的问题啦!

Uncaught SyntaxError: Cannot use import statement outside a module 是一个JS错误。这个错误的原因是在非模块中使用了import语句。 为了解决这个错误,您可以将脚本的类型设置为module,页面中的script标签添加type="module"属性。例如,在index.html文件中可以这样编写脚本代码: ``` <script type="module"> import { add } from './test.js'; console.log(add(1,2)); // 打印出3 </script> ``` 同时,确保导入的模块(test.js)也是一个ES6模块,需要使用export语句将需要导出的函数或变量暴露出来。例如,在test.js文件中可以这样编写代码: ``` export const add = (x, y) => { return x + y; } ``` 通过以上方法,您可以解决Uncaught SyntaxError: Cannot use import statement outside a module错误,并且成功使用import语句在模块中导入其他脚本中的函数或变量。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [报错:Uncaught SyntaxError: Cannot use import statement outside a module 详解](https://download.csdn.net/download/weixin_38517095/14038442)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Uncaught SyntaxError: Cannot use import statement outside a module](https://blog.csdn.net/Amnesiac666/article/details/129103005)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码飞_CC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值