You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome 65环境下无转译器使用ES6模块加载报错求助

解决Chrome 65中ES6模块的语法错误问题

这个错误的核心原因很明确:Chrome默认会把<script>标签里的代码当作传统非模块脚本处理,而这类脚本并不支持import/export这类ES6模块语法——哪怕Chrome 65已经原生支持ES6模块,也需要明确告诉浏览器这是模块脚本才行。

具体修复步骤:

  1. 修改index.html中的script标签
    给引入index.js的script标签加上type="module"属性,让Chrome知道这是一个ES6模块:

    <script type="module" src="js/index.js"></script>
    
  2. 确认模块路径的规范性
    你的index.js里import Main from './classes/Main.js'的路径是正确的,但要注意:在Chrome 65的ES6模块机制中,相对路径必须包含完整的.js扩展名,不能省略,否则可能出现加载失败的问题。

  3. 注意本地测试的跨域限制
    如果是直接在本地打开index.html(使用file://协议),Chrome会因为安全策略阻止模块加载。你需要用本地HTTP服务器来测试,比如:

    • 用Node.js的http-server:先执行npm install -g http-server安装,再在项目根目录运行http-server,通过http://localhost:8080访问
    • 用Python简易服务器:Python 3运行python -m http.server,Python 2运行python -m SimpleHTTPServer

修复后的完整代码示例:

  • index.html:
    <script type="module" src="js/index.js"></script>
    
  • index.js:
    import Main from './classes/Main.js';
    const init = () => { const app = new Main(); };
    init();
    
  • Main.js:
    export default class Main { constructor() { } }
    

这样修改后,Chrome 65就能正确解析ES6模块语法,不会再出现Uncaught SyntaxError: Unexpected identifier错误了。

内容的提问来源于stack exchange,提问作者interwebjill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:22:40