Require
suggest changeVersions
require is the CommonJS loader. It has been in Node from the start. The call shape below has not changed.
Introduction
require() loads a file or an installed package. Node resolves a relative path from the file that calls require, not from the shell's current directory. A bare name such as require('express') is looked up in node_modules.
Syntax
- module.exports = { testFunction: testFunction };
- var test_file = require('./testFile.js'); // a file named testFile.js next to this one
- test_file.testFunction(our_data); // testFile.js assigned testFunction on module.exports
Remarks
module.exports is what the caller receives. If you never assign it, require still runs the file and returns an empty object, so the file can be loaded for side effects. Nothing is exported under the name .export. That is not a Node API.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents