Uncaught SyntaxError: Cannot use import statement outside a module (at hook.js:2:1)
时间: 2023-08-26 13:18:21 浏览: 149
出现报错"Uncaught SyntaxError: Cannot use import statement outside a module"的原因是因为使用了ES6的模块语法import,而在默认的script标签中使用ES6的模块语法会导致浏览器解析错误。解决这个问题的方法是将script标签的type属性改为"module",这样浏览器会将其作为模块导入。你可以修改你的index.html文件的script标签如下所示:
```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>
```
这样修改后,浏览器会正确解析ES6的模块语法,避免报错。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Uncaught SyntaxError: Cannot use import statement outside a module的解决方法](https://blog.csdn.net/cc18868876837/article/details/113915176)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* [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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文