Stats

13 Contributors: 1 Tuesday, July 11, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Require()

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.

Related Examples