Folder as a module

suggest change

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

module.exports = function() {
  return 1;
}
module.exports = function() {
  return 2;
}
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');

If the argument has no ./ or other path, Node loads the name 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"
}

The main file does not have to be named index.js.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents