I have a pretty standard node module using babel to transpile our code which is then output to a 'lib' folder. the package.json points 'main' to 'lib/index.js' so that people can just require('my-module')
However, if I have a subdirectory (say my-module/server for example) then when they use my-module they have to do require('my-module/lib/server')
. I've seen people put post build steps that will copy package.json into lib, but that just feels hacky and wrong to me. Is there any way in npm to specify a main directory whereby any require()'s of my module would start at that directory? Then I can just have users do require('my-module/server') without the lib part...
If I got your question right, the only thing you need is to point your's module main path inside package.json.
Where index.js is your module's main file. For more info check npm documentation here
I think the best solution is that you have as main an index.js and on it all the submodules so you can do did something like
require('your-module').server
You have several options how to accomplish your goal.
Export it all in lib/index.js
In your main module's entry point, you export everything that you would like to expose from your module.
+
Full control over what is part of the public API and what is considered "private"+
Plays really nice with ES2015 modules specification-
Does not satisfy your requirement to require the component (server
) through a directrequire()
call with path to the server moduleExample:
Usage:
Create entry files for the components in your root
You can create separate files for each of the component you would like to expose as part of you public API in your module's root folder.
+
Satisfies your requirement of being able to require a component via specific path-
Slightly more complex structure, requires extra file for each public component-
Does not play well with ES2015 modules (you have to use the full path to the component and cannot use destructuring syntax)Example folder structure:
The my-module/server.js file would only point to the actual implementation:
General notes
It should be noted that while technically possible, relying on full module paths by your consumers is not the best option, because the folder and file structure now becomes part of your API, so moving files around is suddenly a breaking change. Whenever I find myself in need to require a module's component this way, I treat it as if I was messing around with that module's internals and that I should expect my code to break even with a patch-version upgrade of that module.
Let's imagine you have a package
my-module
, and inside you have a subfoldermy-submodule
. Yourmy-module/package.json
file will contain the following line:That will allow your package to be required by the line
To allow using the same syntax for submodule, all you have to do is to put package.json file in your submodule folder. It should contain the following:
After that, you can use simple require syntax:
It's hard without seeing your folder structure, but you can:
module.exports =
to export the babel outputpackage.json
lib/index.js