Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn 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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how to export functions or variables from a module in Node.js?

What happens if the required module does not exist or has an error?

Are there differences between using `require` and `import` in Node.js?

Awesome!

Completion rate improved to 7.14

bookImporting with require

Swipe to show 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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4
some-alt