JavaScript Module Import: From File Inclusion Errors to ES6 Module Solutions

Nov 21, 2025 · Programming · 14 views · 7.8

Keywords: JavaScript Modules | ES6 Import Export | type=module | File Inclusion | Live Bindings

Abstract: This article provides an in-depth exploration of common issues and solutions in JavaScript module imports. Through analysis of a typical file inclusion error case, it explains the working principles of ES6 module systems, including export/import syntax, module type declaration, relative path resolution, and other core concepts. The article offers complete code examples and step-by-step debugging guidance to help developers understand how to properly use JavaScript modules in browser environments.

Problem Analysis: Why is Student Function Undefined?

In the original code, the developer attempted to use the import './course'; statement in the student.js file to import the course.js file, but encountered a "Student is not defined" error when running in the browser. The root cause of this issue lies in incorrect usage of the JavaScript module system.

ES6 Module System Fundamentals

ES6 introduced a standard module system that allows developers to split code into independent module files. The module system is based on two core concepts: export and import. To use module functionality, files must be explicitly declared as module types.

Solution: Correct Module Import Approach

Here is the complete corrected code implementation:

1. Exporting Module Functions

In the course.js file, use the export keyword to explicitly export functions:

export function Course() {
    this.id = '';
    this.name = '';
}

2. Importing Dependency Modules

In the student.js file, use the import statement to import the Course function:

import { Course } from './course.js';

export function Student() {
    this.firstName = '';
    this.lastName = '';
    this.course = new Course();
}

3. Declaring Module Type in HTML

The most critical step is using the type="module" attribute in the HTML file:

<div id="myDiv"></div>
<script type="module">
    import { Student } from './models/student.js';
    
    window.onload = function () {
        var x = new Student();
        x.course.id = 1;
        document.getElementById('myDiv').innerHTML = x.course.id;
    }
</script>

Core Concept Analysis

Importance of Module Type Declaration

The type="module" attribute informs the browser that the script should be treated as a module. Modules have the following characteristics:

Export and Import Syntax

ES6 modules support various export and import methods:

Path Resolution Rules

In the module system, path resolution follows specific rules:

Common Errors and Debugging Techniques

Error Type Analysis

Common errors developers encounter:

Debugging Recommendations

When encountering module-related issues:

Advanced Usage

Dynamic Import

In addition to static imports, ES6 supports dynamic imports:

// Dynamic import example
async function loadModule() {
    const module = await import('./models/student.js');
    const Student = module.Student;
    return new Student();
}

Live Bindings in Modules

ES6 modules use live bindings, meaning imported values update as they change in the exporting module:

// Exporting module
export let counter = 0;

export function increment() {
    counter++;
}

// Importing module
import { counter, increment } from './counter.js';

console.log(counter); // 0
increment();
console.log(counter); // 1 - value updated

Browser Compatibility Considerations

While modern browsers support ES6 modules, production environments require attention to:

Conclusion

Proper usage of ES6 module systems requires understanding several key points: must use type="module" to declare module type, correctly use export and import statements, ensure paths include file extensions. By following these specifications, developers can fully leverage modularization advantages to build more maintainable and reusable JavaScript code.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.