NPM包装没有切入点
我计划将JavaScript库作为NPM软件包发布,我对软件包的入口点和最佳实践有疑问:当软件包不揭露单个入口点时,最好的做法是什么?
- 显式声明
导出
没有main
- 用
带有
,该将指向main
的导出index.js
代码>会抛出一些有用的错误消息,解释如何
仅与node&gt兼容我的lib我的lib; 14,我们使用enfort
属性来利用subpath offorts(因为它作为ESM/CJS模块格式发货)
如声明,该库不会公开一个入口点,并且应由从特定文件导入默认导出。因此,消费者将使用以下语义进行导入:
// ESM
import Foo from 'my-lib/path/filename1';
// CJS
const Foo = require('my-lib/path/filename1');
I plan to publish a JavaScript library as an npm package and I have a question regarding packages entry points and best practices: what would be the best practice when a package doesn't expose a single entry point?
- Explicitly declare
exports
withoutmain
- Declare
exports
withmain
that would point to anindex.js
that would throw some helpful error message explaining how to consume the lib
My lib only is only compatible with node > 14 and we use the exports
property to leverage subpath exports (as it's shipped both as ESM/CJS module format)
As declared, the library doesn't expose a single entry point and is meant to be consumed by importing a default export from a specific file. So the consumer would use the following semantics to import:
// ESM
import Foo from 'my-lib/path/filename1';
// CJS
const Foo = require('my-lib/path/filename1');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论