Importing with import
When using ES Modules in Node.js, you use the import statement to bring in functions, objects, or values from other modules. The import statement supports several variations depending on how the module exports its content.
mathUtils.js
main.js
- If a module uses
export default, you can import the default export by giving it any name you choose, as shown byimport PI from './mathUtils.js'; - For named exports, you use curly braces and specify the exact names, such as
import { add, subtract } from './mathUtils.js'; - You can also combine both forms in a single statement:
import PI, { add, subtract } from './mathUtils.js'. This is called destructuring imports, and it allows you to select only the exports you need from a module.
The import statement must always be at the top level of your file and cannot be used conditionally or inside functions. This syntax helps you write modular, maintainable code that clearly lists dependencies at the beginning of each module.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 7.14
Importing with import
Deslize para mostrar o menu
When using ES Modules in Node.js, you use the import statement to bring in functions, objects, or values from other modules. The import statement supports several variations depending on how the module exports its content.
mathUtils.js
main.js
- If a module uses
export default, you can import the default export by giving it any name you choose, as shown byimport PI from './mathUtils.js'; - For named exports, you use curly braces and specify the exact names, such as
import { add, subtract } from './mathUtils.js'; - You can also combine both forms in a single statement:
import PI, { add, subtract } from './mathUtils.js'. This is called destructuring imports, and it allows you to select only the exports you need from a module.
The import statement must always be at the top level of your file and cannot be used conditionally or inside functions. This syntax helps you write modular, maintainable code that clearly lists dependencies at the beginning of each module.
Obrigado pelo seu feedback!