Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Using require to Import Modules | Node.js Architecture and the Module System
Node.js Foundations

bookUsing require to Import Modules

Node.js uses the CommonJS module system to allow you to organize code into separate files, or modules, and reuse them throughout your application. The core of this system is the require function, which lets you load both built-in Node.js modules and your own custom modules. When you use require, Node.js reads the specified file, executes its code, and returns the value of module.exports from that file. This helps you keep your codebase modular, maintainable, and easier to test.

The require function works synchronously, meaning it will block further code execution until the module is loaded. You can use it to import modules by providing either the name of a built-in module or a relative path to a custom module. Built-in modules, such as fs for file system operations, come bundled with Node.js and do not require installation. Custom modules are files you create in your project, usually ending with .js.

index.js

index.js

copy
Note
Study more

In custom modules, you use module.exports to specify which functions, objects, or values should be accessible when the module is imported with require. Only what is assigned to module.exports will be available to other files.

question mark

Which statement best describes how the require function works in Node.js?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you give an example of how to use `require` with a custom module?

What is the difference between CommonJS and ES modules in Node.js?

How do I export multiple functions from a custom module?

Awesome!

Completion rate improved to 9.09

bookUsing require to Import Modules

Swipe um das Menü anzuzeigen

Node.js uses the CommonJS module system to allow you to organize code into separate files, or modules, and reuse them throughout your application. The core of this system is the require function, which lets you load both built-in Node.js modules and your own custom modules. When you use require, Node.js reads the specified file, executes its code, and returns the value of module.exports from that file. This helps you keep your codebase modular, maintainable, and easier to test.

The require function works synchronously, meaning it will block further code execution until the module is loaded. You can use it to import modules by providing either the name of a built-in module or a relative path to a custom module. Built-in modules, such as fs for file system operations, come bundled with Node.js and do not require installation. Custom modules are files you create in your project, usually ending with .js.

index.js

index.js

copy
Note
Study more

In custom modules, you use module.exports to specify which functions, objects, or values should be accessible when the module is imported with require. Only what is assigned to module.exports will be available to other files.

question mark

Which statement best describes how the require function works in Node.js?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 2
some-alt