最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change NPM entry point without an index.js - Stack Overflow

programmeradmin4浏览0评论

I recently published a private module to NPM which has some mon code we use across a few of our services.

The code is written in ES6 and so we need to transpile it using babel before publishing to NPM. I've got a prepublish script which transpiles src in to lib.

There's no index.js file in this module since it's just some mon code.

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). I instead have to use 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. One workaround I can see would be to export all the mon code in an index.js file, but this isn't maintainable at all.

I recently published a private module to NPM which has some mon code we use across a few of our services.

The code is written in ES6 and so we need to transpile it using babel before publishing to NPM. I've got a prepublish script which transpiles src in to lib.

There's no index.js file in this module since it's just some mon code.

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). I instead have to use 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. One workaround I can see would be to export all the mon code in an index.js file, but this isn't maintainable at all.

Share Improve this question asked Jan 6, 2017 at 12:32 Rhys CammRhys Camm 1112 silver badges4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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.

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?

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?

发布评论

评论列表(0)

  1. 暂无评论