File Structure: Requiring Sub-Modules in Node.js
Short answer: you can't.
Long answer: You should either directly use the second directory structure you proposed (/myModule/submodules/
) or add some kind of API to your main exports (index.js) to quickly get the desired module.
While you can technically call require('myModule/some/complex/path')
, the Node.js / npm packages standard is to rely on the unique interface provided by require('myModule')
.
// /dist/node/index.js
var path = require('path');
exports.require = function (name) {
return require(path.join(__dirname, name));
};
Then in your app:
var myModule = require('myModule');
var submodule1 = myModule.require('submodules/submodule1');