Module A
│
└─ Module B (devDependency in Module A's package.json)
│
└─ Module C (dependency in Module B's package.json)
Module B is what I am developing. But I know that module C would be called in Module A with require('Module C')
. And the error I am having is Cannot find module 'Module C'
.
My current solution is ugly which is:
In index.js of Module B
exports.Module_C = require('Module_C) || require(path.resolve(__dirname, 'node_modules/Module_C/index');
In Module A
require('Module_B').Module_C
I am hoping there is better way to do it.
Modules are not inherited.
If you need the same module in two different places in your system, just require it in two different places.
// A.js
var C = require("./modules/c");
var B = require("./modules/b");
// B.js
var C = require("./modules/c");
module.exports = { };
// C.js
module.exports = { };
If you need to import subsections of a module, then navigate to the file (or root of the files) that you need.
// app.js
var add = require("./lib/helpers/math/add");
If you are trying to offer the functionality of multiple modules inside of one module, then what you're looking for is a service, which you will create an API for, to abstract what you want the end user to be able to do, from all of the places you need to touch, in order to do it.
Even if that service / library is basically just extending other interfaces onto its own interface.