Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Importing with require | Understanding and Using Node.js Modules
Working with Modules and Packages in Node.js

bookImporting with require

To use code from another file in Node.js, you use the require function. This function allows you to import modules and access their exported values or functions. The require function is built into Node.js, and its syntax is straightforward: you pass the path to the module you want to load as a string, and require returns the module's exported object. The syntax looks like this: const moduleName = require('./modulePath');. The path can be relative or absolute, and Node.js will resolve it to load the correct file.

When you call require, Node.js loads the module synchronously. This means your code will pause until the module has finished loading and any exported values are available for use. If the same module is required multiple times, Node.js will only load it once and then cache the result. All subsequent calls to require for that module will return the cached exports, not reload the file.

main.js

main.js

greet.js

greet.js

copy
question mark

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

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 4

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 7.14

bookImporting with require

Glissez pour afficher le menu

To use code from another file in Node.js, you use the require function. This function allows you to import modules and access their exported values or functions. The require function is built into Node.js, and its syntax is straightforward: you pass the path to the module you want to load as a string, and require returns the module's exported object. The syntax looks like this: const moduleName = require('./modulePath');. The path can be relative or absolute, and Node.js will resolve it to load the correct file.

When you call require, Node.js loads the module synchronously. This means your code will pause until the module has finished loading and any exported values are available for use. If the same module is required multiple times, Node.js will only load it once and then cache the result. All subsequent calls to require for that module will return the cached exports, not reload the file.

main.js

main.js

greet.js

greet.js

copy
question mark

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

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 4
some-alt