Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Introduction to import Syntax | Node.js Architecture and the Module System
Node.js Foundations

bookIntroduction to import Syntax

When you work with Node.js, you will often need to organize your code into multiple files. To use code from one file in another, you need a way to import and export functionality. Node.js supports two main module systems: CommonJS (using require) and ES modules (using import). The ES module system, based on the JavaScript standard, is becoming increasingly popular and is recommended for modern Node.js projects.

The ES module syntax uses the import statement to bring in code from other files. You should use ES modules when you want compatibility with modern JavaScript tools, or when you are writing code that may run in both Node.js and browsers. ES modules are especially useful if you are using features like import/export, top-level await, or want to avoid some of the quirks of CommonJS.

index.js

index.js

copy
Note
Note

The CommonJS module system uses require() to load modules and module.exports or exports to export them. In contrast, ES modules use import and export statements. ES modules are statically analyzed, which enables better optimization and tooling, while CommonJS modules are loaded dynamically at runtime. Also, ES modules require the .mjs file extension or "type": "module" in your package.json to work in Node.js.

question mark

Which of the following statements best describes a key difference between require (CommonJS) and import (ES modules) in Node.js?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 9.09

bookIntroduction to import Syntax

Desliza para mostrar el menú

When you work with Node.js, you will often need to organize your code into multiple files. To use code from one file in another, you need a way to import and export functionality. Node.js supports two main module systems: CommonJS (using require) and ES modules (using import). The ES module system, based on the JavaScript standard, is becoming increasingly popular and is recommended for modern Node.js projects.

The ES module syntax uses the import statement to bring in code from other files. You should use ES modules when you want compatibility with modern JavaScript tools, or when you are writing code that may run in both Node.js and browsers. ES modules are especially useful if you are using features like import/export, top-level await, or want to avoid some of the quirks of CommonJS.

index.js

index.js

copy
Note
Note

The CommonJS module system uses require() to load modules and module.exports or exports to export them. In contrast, ES modules use import and export statements. ES modules are statically analyzed, which enables better optimization and tooling, while CommonJS modules are loaded dynamically at runtime. Also, ES modules require the .mjs file extension or "type": "module" in your package.json to work in Node.js.

question mark

Which of the following statements best describes a key difference between require (CommonJS) and import (ES modules) in Node.js?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3
some-alt