NPM包装没有切入点

发布于 2025-01-23 12:16:43 字数 579 浏览 5 评论 0原文

我计划将JavaScript库作为NPM软件包发布,我对软件包的入口点和最佳实践有疑问:当软件包不揭露单个入口点时,最好的做法是什么?

  1. 显式声明导出没有main
  2. 带有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?

  1. Explicitly declare exports without main
  2. Declare exports with main that would point to an index.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文