🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
🪟 Windows TippsK-Lite Mega Codec Pack(12.09.2026 um 12:00 Uhr)
🕵️ SicherheitslückenDefender 0-Day ShieldBreak (CVE-2026-69414) nicht sauber gepatcht - BornCity(11.09.2026 um 12:52 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
🪟 Windows TippsK-Lite Mega Codec Pack(12.09.2026 um 12:00 Uhr)
🕵️ SicherheitslückenDefender 0-Day ShieldBreak (CVE-2026-69414) nicht sauber gepatcht - BornCity(11.09.2026 um 12:52 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

How to Include a JavaScript File in Another JavaScript File

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Including one JavaScript file in another is a fundamental concept in JavaScript development. It allows you to break your code into smaller, reusable modules for better organization and maintainability. There are different methods for achieving this based on the environment (browser or Node.js) and the version of JavaScript being used.



Here’s a complete guide to help you understand the various ways to include a JavaScript file in another, along with examples.









1. Using ES6 Modules (import and export)



This is the modern and recommended way to include one JavaScript file in another. It allows you to export code (functions, constants, or classes) from one file and import it into another.






Example:



File 1: math.mjs




CODE
// Export a function and a constant
export function add(n1, n2) {
return n1 + n2;
}
export const pi = 3.14159;






File 2: main.mjs




CODE
// Import the function and constant
import { add, pi } from './math.mjs';

console.log(add(2, 3)); // Output: 5
console.log(pi); // Output: 3.14159






Key Notes:




  • Use the .mjs file extension or set "type": "module" in your package.json file if you're working in a Node.js environment.

  • Relative paths (./ or ../) are mandatory unless importing from a package.

  • This method supports tree-shaking, which removes unused code during bundling.









2. Using require() (CommonJS – Node.js)



In Node.js, the CommonJS module system is commonly used. This method relies on module.exports and require() for including files.






Example:



File 1: math.js




CODE
// Export using CommonJS syntax
function add(n1, n2) {
return n1 + n2;
}
const pi = 3.14159;

module.exports = { add, pi };






File 2: main.js




CODE
// Import using require()
const { add, pi } = require('./math.js');

console.log(add(2, 3)); // Output: 5
console.log(pi); // Output: 3.14159






Key Notes:




  • Use require() in Node.js or environments that support CommonJS.

  • This method is not natively supported in modern browsers without bundlers like Webpack or Rollup.









3. Using <script> Tags in HTML (Browser-based JavaScript)



For browser environments, you can include multiple JavaScript files directly using <script> tags in your HTML file.






Example:



HTML File: index.html




CODE
<!DOCTYPE html>
<html lang="en">
<head>
<script src="math.js"></script>
<script src="main.js"></script>
</head>
<body>
<h1>JavaScript File Inclusion</h1>
</body>
</html>






File 1: math.js




CODE
function add(n1, n2) {
return n1 + n2;
}
const pi = 3.14159;






File 2: main.js




CODE
console.log(add(2, 3)); // Output: 5
console.log(pi); // Output: 3.14159






Key Notes:




  • The order of the <script> tags matters, as scripts are executed sequentially.

  • To use ES6 modules in the browser, include the type="module" attribute:




CODE
  <script type="module" src="main.mjs"></script>












4. Using Dynamic Imports (ES6)



Dynamic imports allow you to load JavaScript modules at runtime, which is useful for lazy loading or conditionally including files.






Example:






CODE
// Dynamically import the module
import('./math.mjs').then((module) => {
console.log(module.add(2, 3)); // Output: 5
console.log(module.pi); // Output: 3.14159
});






Key Notes:





  • import() returns a promise that resolves to the module.

  • Useful for optimizing performance in large applications by loading code only when needed.









Choosing the Right Method




























Scenario Recommended Method
Modern JavaScript development ES6 Modules (import/export)
Node.js environment require() (CommonJS)
Browser without bundlers <script> tags
Dynamic or conditional imports Dynamic import() (ES6)








Conclusion



Including JavaScript files in other JavaScript files is a foundational concept in web development. The method you choose depends on the environment you're working in and the needs of your project. For modern development, ES6 modules are the go-to solution, offering powerful features like tree-shaking and dynamic imports. For legacy or Node.js projects, require() and <script> tags remain viable options.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
4 Quellen
CVE-2026-71328 | Microsoft .NET/Visual Studio buffer overflow (Nessus ID 344808)
3 Quellen
CVE-2026-65669 | Microsoft SQL Server up to 22.8.1 injection (Nessus ID 344797)
1 Quelle
CVE-2026-75650 | Adobe Commerce/Commerce B2B/Magento Open Source Template Engine StyleSmuggler neutralization (EUVD-2026-72530 / Nessus ID 344801)
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to Include a JavaScript File in Another JavaScript File

Thematisch verwandte Begriffe: Include, JavaScript, File, Another · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...