Node.js Require()

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

This documentation focuses on explaining the uses and of the require() statement that NodeJS includes in their language.

Require is an import of certain files or packages used with NodeJS's modules. It is used to improve code structure and uses. require() is used on files that are installed locally, with a direct route from the file that is require'ing.

Syntax

  • module.exports = {testFunction: testFunction};
  • var test_file = require('./testFile.js'); //Let us have a file named testFile
  • test_file.testFunction(our_data); //Let testFile have function testFunction

Remarks

Using require() allows code to be structured in a way similar to Java's use of classes and public methods. If a function is .export'ed, it can be require'ed in another file to be used. If a file is not .export'ed, it cannot be used in another file.



Got any Node.js Question?