Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026
The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.
When using ES Modules with import statements in the browser, you might see this error:
Uncaught SyntaxError: Unexpected identifier
The problem
This happens when the browser tries to load a JavaScript file that uses ES Module syntax (import/export) but the script isn’t marked as a module.
<!-- This won't work with import/export -->
<script src="index.js"></script>
The browser loads the file as a regular script, sees the import statement, and throws an error because import isn’t valid syntax in non-module scripts.
The solution
Add type="module" to your script tag:
<script type="module" src="index.js"></script>
Now the browser knows to treat this file as an ES Module, and import/export statements will work.
Example
Your HTML:
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
</head>
<body>
<script type="module" src="index.js"></script>
</body>
</html>
Your JavaScript (index.js):
import { greet } from './utils.js'
greet('World')
Your module (utils.js):
export function greet(name) {
console.log(`Hello, ${name}!`)
}
Important notes about modules
-
File extensions: When importing, you often need to include the file extension (
.js)import { func } from './mymodule.js' // Include .js -
CORS: Modules are fetched with CORS, so you can’t test them by opening the HTML file directly. Use a local server:
npx serve . # or python -m http.server -
Strict mode: Modules always run in strict mode
-
Scope: Variables in modules are scoped to the module, not global
-
Defer by default: Module scripts are deferred automatically, so they won’t block HTML parsing
In Node.js
For Node.js, you have two options to use ES Modules:
- Use the
.mjsextension for your files - Add
"type": "module"to yourpackage.json:{ "type": "module" }