[英]Change NPM entry point without an index.js
I recently published a private module to NPM which has some common code we use across a few of our services. 我最近向NPM发布了一个私有模块,其中包含一些我们在一些服务中使用的通用代码。
The code is written in ES6 and so we need to transpile it using babel before publishing to NPM. 该代码是用ES6编写的,因此在发布到NPM之前,我们需要使用babel对其进行编译。 I've got a prepublish script which transpiles src
in to lib
. 我有一个预发布脚本,可将src
为lib
。
There's no index.js
file in this module since it's just some common code. 该模块中没有index.js
文件,因为它只是一些通用代码。
The problem I'm having is that when I install the module from NPM, using require('@ourorg/ourmodule/somecode')
doesn't work (module can't be found). 我遇到的问题是,当我从NPM安装模块时,使用require('@ourorg/ourmodule/somecode')
不起作用(找不到模块)。 I instead have to use require('@ourorg/ourmodule/lib/somecode')
. 我不得不使用require('@ourorg/ourmodule/lib/somecode')
。
I've tried changing the main
field in package.json
to many variations of lib
, but it doesn't seem to work unless I include an index.js
file, in which case require('@ourorg/ourmodule')
returns whatever is exported there. 我试图将package.json
的main
字段更改为lib
许多变体,但是除非我包含index.js
文件,否则它似乎无法工作,在这种情况下require('@ourorg/ourmodule')
返回什么出口到那里。 One workaround I can see would be to export all the common code in an index.js
file, but this isn't maintainable at all. 我可以看到的一种解决方法是将所有通用代码导出到index.js
文件中,但这根本无法维护。
The main
field in package.json
follows the same rules as normal Node imports - either it should point at a single file in particular, or it can point at a directory that has an index.js
in it. package.json
的main
字段遵循与普通Node导入相同的规则-它应该特别指向单个文件,或者可以指向其中包含index.js
的目录。
As far as I know, there is no way to make importing your package simply be an alias for a directory. 据我所知,没有办法使导入包仅仅是目录的别名。 If there was, what would require("@ourorg/ourmodule")
return? 如果存在, require("@ourorg/ourmodule")
返回什么?
If it's absolutely driving you crazy having to type lib
every time you import something, maybe you could add a step to your build process that autogenerates an index.js
that re-exports everything at the root? 如果这绝对使您发疯,每次导入内容时都必须输入lib
,那么也许可以在构建过程中添加一个步骤,以自动生成index.js
并重新导出根目录中的所有内容?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.