简体   繁体   English

隐藏 npm package 中的特定导出

[英]Hide specific exports in the npm package

I am new to creating npm packages.我是创建 npm 包的新手。 I would not want users to be able to import everything from my package, such as the Utils class.我不希望用户能够从我的 package 中导入所有内容,例如Utils class。

export class Utils implements IUtils {
  // ......
}

How do I avoid it?我该如何避免呢?

You can specify the exports key in your package.json .您可以在package.json中指定exports键。 When this is set, only the specified paths can be accessed from the module.设置后,只能从模块访问指定的路径。

Docs: https://nodejs.org/api/packages.html#exports文档: https://nodejs.org/api/packages.html#exports

The way I usually do this is to create an index.js or index.ts and add that to the exports .我通常这样做的方法是创建一个index.jsindex.ts并将其添加到exports Then, anything I want to expose publicly, I export via the index file.然后,我想公开公开的任何内容,我都会通过索引文件导出。

For example:例如:

// index.ts
export { MyPublicClass } from './MyPublicClass'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM