Chrome v61中ES6 import报Unexpected identifier错误求助
Unexpected identifier Error with ES6 import in Chrome v61 Hey there! I totally get why this is confusing—you’ve got ES6 working fine in your ex.js file, but as soon as you add an import statement, Chrome throws that frustrating Unexpected identifier error. Let’s walk through the most likely fixes step by step:
1. You’re not loading your script as an ES module
Chrome v61 does support ES6 modules, but only if you tell the browser to treat your script as one. By default, <script> tags load files in "classic script" mode, which doesn’t recognize import/export syntax at all.
Fix:
Update your script tag in HTML to include the type="module" attribute:
<script type="module" src="./js/ex.js"></script>
This tells Chrome to parse the file as an ES module, where import/export are valid syntax. Also, keep in mind that modules run in strict mode automatically, so you don’t need to add 'use strict' manually.
2. Double-check your import path
A tiny typo in your path can easily trigger this error. Chrome is strict about file paths, especially when running locally:
- Make sure the path in your
importmatches the actual file location exactly (case-sensitive!audiohandler.js≠AudioHandler.js) - Use full relative paths:
import AudioHandler from './js/AudioHandler.js'(don’t omit the./or the.jsextension—modules require explicit extensions in most cases) - Verify that
AudioHandler.jsactually exists in the./js/directory, and that it’s exporting something correctly (e.g.,export default class AudioHandler { ... })
3. Local file:// protocol limitations
If you’re opening your HTML file directly from your computer (using the file:// URL), Chrome blocks certain module behaviors due to cross-origin security restrictions. The error message might be misleading, but the root issue is the browser’s security rules for local files.
Fix:
Run your code on a local web server instead. Here are quick ways to do this:
- If you have Python 3 installed: Open your project folder in terminal and run
python -m http.server - If you have Node.js installed: Run
npx servein your project folder
Then access your page viahttp://localhost:8000(or the port your server uses) instead of opening the file directly.
4. Verify Chrome’s module support (just to be sure)
While Chrome 61 should have ES module support enabled by default, it’s worth a quick check:
- Type
chrome://flags/#enable-javascript-harmonyinto your address bar - Make sure the "Experimental JavaScript" flag is set to "Enabled" (though this is default in 61)
Quick recap of steps to try:
- Add
type="module"to your script tag - Validate your import path and confirm the target file exists
- Switch to a local web server instead of using the
file://protocol
That should get your import statement working properly in Chrome v61!
内容的提问来源于stack exchange,提问作者Kornelia

