Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Structure: Requiring Sub-Modules in Node.js

I have the following Node.js module/npm package:

|- dist/
|- - requirejs/
|- - - [stuff in amd pattern ...]
|- - node/
|- - - index.js
|- - - submodules/
|- - - - submodule1.js
|- - - - [submodule2.js etc.]
|- package.json
|- README.md

I can require dist/node/index.js via the module name (because I set it as the main entry-point file in package.json) like so:

var myModule = require('myModule');

I would like to require the submodule (as in AMD pattern) by doing so:

var mySubmodule = require('myModule/submodules/submodule1');

This throws an error in Node.js. The problem is, that Node.js requires the main file from its dist/node/ subdirectory but still keeps the modules root as the working directory.

Assuming the following structure would be present:

|- dist/
|- - node/
|- - - index.js
|- submodules/
|- - submodule1.js
|- package.json
|- README.md

Now doing require('myModule/submodules/submodule1') would work.

NOW THE QUESTION: Is there any setting/config to set the "module namespace" or working directory to the directory where the main file is in, or do I really need to put the submodules folder into the project root, to make it accessible without doing require('myModule/dist/node/submodules/submodule1')?

like image 565
headacheCoder Avatar asked Sep 09 '15 11:09

headacheCoder


People also ask

How do you require the file system module in a Node.js program?

The Node.js file system module allows you to work with the file system on your computer. To include the File System module, use the require() method: var fs = require('fs');

Where does require look for modules?

If a core module can't be found, Node assumes the identifier refers to a Node package. When attempting to locate a Node package, require() looks for a node_modules directory in the current directory and checks it for the package.

Do I need all Node modules?

To have a node modules in each project is totally optional. You can install package globally and use in you project. But the problem is when packages got updated code may break. So it is always preferable to have node modules specific to each project.

Which module includes methods to deal with file paths?

Many people forget about one of Node's most useful built-in modules, the path module. It's a module with methods that help you deal with file and directory path names on the machine's filesystem.


1 Answers

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');
like image 178
Crogo Avatar answered Sep 27 '22 18:09

Crogo