节点模块的根目录 [英] root directory for node module
问题描述
我有一个非常漂亮的标准节点模块,使用babel来转换我们的代码,然后将其输出到"lib"文件夹中. package.json将'main'指向'lib/index.js',以便人们可以require('my-module')
但是,如果我有一个子目录(例如说my-module/server),那么当他们使用my-module时,它们必须执行require('my-module/lib/server')
.我见过人们把构建后的步骤放到将package.json复制到lib的过程中,但这对我来说是很棘手和错误的. npm中是否有任何方法可以指定主目录 ,因此模块的所有require()都将从该目录开始?然后我可以让用户不用lib部分来执行require('my-module/server')...
我认为最好的解决方案是将index.js及其所有子模块作为主体,以便您可以执行require('your-module').server
>
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...
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
这篇关于节点模块的根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!