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

bookHow Node.js Loads Modules

To work effectively with modules in Node.js, you need to understand how Node.js finds and loads them. This process is called module resolution. When you use the require function to import a module, Node.js follows a specific algorithm to locate the file or package you are requesting. The resolution process takes into account file extensions, folder structure, and even the presence of special files like package.json.

Node.js first checks if the module you are requiring is a core module, such as fs or http. If it is not a core module, Node.js treats the string you pass to require as either a relative path (starting with ./ or ../), an absolute path, or a module name. For relative and absolute paths, Node.js tries to resolve the file by checking the following in order:

  • If the exact file exists with the given name;
  • If the file exists with a .js extension added;
  • If the file exists with a .json extension added;
  • If the file exists with a .node extension added.

If the path points to a directory, Node.js will look for a package.json file inside that directory. If it finds one, it will use the main field to determine which file to load. If there is no package.json or no main field, Node.js will look for an index.js, index.json, or index.node file inside the directory.

If you require a module by name (not a path), Node.js will search for it in the node_modules folders, starting from the current directory and then moving up the directory tree until it reaches the root.

app.js

app.js

copy
Note
Note

Node.js caches modules after the first time they are loaded. This means that if you require the same module multiple times, it is only executed once, later calls return the same cached object. This improves performance and consistency across your application.

question-icon

Fill in the blanks:
When you use require('./moduleName') in Node.js, the module resolution algorithm checks in the following order:

  1. If a file named moduleName exists;
  2. If a file named moduleName.___1___ exists;
  3. If a file named moduleName.___2___ exists;
  4. If a file named moduleName.___3___ exists.

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2

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 Node.js resolves modules from the `node_modules` folder in more detail?

What happens if Node.js can't find the module I'm requiring?

Can you give an example of how the resolution process works with a custom module?

Awesome!

Completion rate improved to 7.14

bookHow Node.js Loads Modules

Swipe to show menu

To work effectively with modules in Node.js, you need to understand how Node.js finds and loads them. This process is called module resolution. When you use the require function to import a module, Node.js follows a specific algorithm to locate the file or package you are requesting. The resolution process takes into account file extensions, folder structure, and even the presence of special files like package.json.

Node.js first checks if the module you are requiring is a core module, such as fs or http. If it is not a core module, Node.js treats the string you pass to require as either a relative path (starting with ./ or ../), an absolute path, or a module name. For relative and absolute paths, Node.js tries to resolve the file by checking the following in order:

  • If the exact file exists with the given name;
  • If the file exists with a .js extension added;
  • If the file exists with a .json extension added;
  • If the file exists with a .node extension added.

If the path points to a directory, Node.js will look for a package.json file inside that directory. If it finds one, it will use the main field to determine which file to load. If there is no package.json or no main field, Node.js will look for an index.js, index.json, or index.node file inside the directory.

If you require a module by name (not a path), Node.js will search for it in the node_modules folders, starting from the current directory and then moving up the directory tree until it reaches the root.

app.js

app.js

copy
Note
Note

Node.js caches modules after the first time they are loaded. This means that if you require the same module multiple times, it is only executed once, later calls return the same cached object. This improves performance and consistency across your application.

question-icon

Fill in the blanks:
When you use require('./moduleName') in Node.js, the module resolution algorithm checks in the following order:

  1. If a file named moduleName exists;
  2. If a file named moduleName.___1___ exists;
  3. If a file named moduleName.___2___ exists;
  4. If a file named moduleName.___3___ exists.

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2
some-alt