Node.js Exporting and Consuming Modules Folder as a module

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!

Example

Modules can be split across many .js files in the same folder. An example in a my_module folder:

function_one.js

module.exports = function() {
  return 1;
}

function_two.js

module.exports = function() {
  return 2;
}

index.js

exports.f_one = require('./function_one.js');
exports.f_two = require('./function_two.js');

A module like this one is used by referring to it by the folder name:

var split_module = require('./my_module');

Please note that if you required it by omitting ./ or any indication of a path to a folder from the require function argument, Node will try to load a module from the node_modules folder.

Alternatively you can create in the same folder a package.json file with these contents:

{
    "name": "my_module",
    "main": "./your_main_entry_point.js"
}

This way you are not required to name the main module file "index".



Got any Node.js Question?