How do I include a JavaScript file in another JavaScript file?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Including one JavaScript file in another is really a module-loading problem, and the right approach depends on runtime environment. Browsers, Node.js, and build tools support different module systems with different syntax and behavior. Using the correct system avoids path bugs, duplicate execution, and deployment surprises.
Use ES Modules in Modern JavaScript
For modern browser and Node.js code, ES modules are the default pattern.
math.js:
main.js:
Browser HTML needs module script type.
Without module script type, browser treats import as syntax error.
Default Exports Versus Named Exports
A module can export one default value and multiple named values. Keep style consistent within a codebase.
Named exports improve refactor safety for large modules, while default exports are sometimes convenient for single-primary modules.
Node.js: ES Modules and CommonJS
Node.js supports both systems, but mixing them requires care.
CommonJS style
ES module style in Node
Set module type in package configuration or use .mjs extension.
Choose one system per package where possible to keep tooling simple.
Script Tags Without Modules
If you are not using modules, script load order in HTML matters.
main.js can use globals declared by lib.js only if lib.js loaded first. This approach works but scales poorly because global namespace collisions become likely in large apps.
Dynamic Loading with import
When code should load on demand, use dynamic import.
This can reduce initial bundle size and improve startup performance.
Build Tools and Path Resolution
In real apps, bundlers such as Vite, Webpack, and Rollup often handle import resolution and transpilation. Even then, module semantics remain the same.
Useful rules:
- use explicit relative paths for local files
- avoid circular dependencies
- keep side-effect modules clearly documented
Example side-effect import:
This pattern is valid but should be used intentionally because import execution order can influence app startup behavior.
Browser Security and CORS Notes
When testing modules in browser, serving files over HTTP is usually required. Opening file URLs directly can fail with module import restrictions.
Quick local server example:
Then open provided localhost URL. This avoids cross-origin and file-origin module loading problems.
Debugging Failed Imports
When import fails, check these first:
- path spelling and extension
- module type configuration
- server response status code
- case-sensitivity differences across operating systems
In browser dev tools, network tab often reveals wrong path or MIME errors immediately.
Common Pitfalls
A common pitfall is using import in browser without type="module". Another is mixing CommonJS and ES module syntax in the same file without interoperability handling. Teams also often omit file extension in browser imports and get failed network requests. Finally, relying heavily on global script order works for small pages but becomes fragile in larger codebases.
Summary
- Prefer ES modules for modern JavaScript in browser and Node.js.
- Use
requireonly in CommonJS codebases or legacy environments. - Configure module type correctly in HTML and Node package settings.
- Use dynamic import for on-demand code loading.
- Debug import failures through path, config, and network checks first.

