Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Importing with import | Modern Modules and Core Node Utilities
Working with Modules and Packages in Node.js

bookImporting 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

mathUtils.js

main.js

main.js

copy
  • If a module uses export default, you can import the default export by giving it any name you choose, as shown by import 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.

question mark

Which of the following correctly imports both the default export and a named export from a module called 'tools.js'?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain the difference between default and named exports?

Are there any restrictions on where I can place import statements in my code?

Can you give more examples of using import statements with ES Modules?

Awesome!

Completion rate improved to 7.14

bookImporting 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

mathUtils.js

main.js

main.js

copy
  • If a module uses export default, you can import the default export by giving it any name you choose, as shown by import 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.

question mark

Which of the following correctly imports both the default export and a named export from a module called 'tools.js'?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2
some-alt