简体   繁体   English

如何在npm包中公开多个文件?

[英]How to expose more than one file in an npm package?

I have an npm package. 我有一个npm包。 Let's say example-package . 假设example-package This is normal way of importing. 这是正常的导入方式。

import RootModule from "example-package";

Now I have one more file nested here. 现在,我在这里嵌套了另一个文件。

Package Root > src > Feature > index.js 包根目录> src>功能> index.js

Now if I have to import this Feature, I would do this. 现在,如果我必须导入此功能,则可以执行此操作。

import Feature from "example-package/src/Feature";

What can I do to avoid developers using my npm package from writing long nested paths and they use something like this. 如何避免使用npm软件包的开发人员编写长嵌套路径,而他们却使用类似的方法。

import Feature from "example-package/Feature";

Just to make it clear, Feature exports multiple options - { A, B ..} . 为了清楚起见, Feature导出多个选项- { A, B ..} I do not want to import Feature from package and again extract options from Feature . 我不想从包中导入Feature并再次从Feature提取选项。 Just want to import it with one slash no matter how long the path is! 无论路径多长,都只需要一个斜杠就可以导入它!

You can add the feature as an export of your index - 您可以将功能添加为索引的导出-

index.js: index.js:

import Feature from './Feature.js'
export Feature

Then anyone using the package can just import like 然后任何使用该软件包的人都可以像

import { Feature } from 'example-package'

I found a solution online. 我在网上找到了解决方案。 Possible solution would be to create a file /Feature/index.js in the root folder with following content. 可能的解决方案是在根文件夹中创建具有以下内容的文件/Feature/index.js

module.exports = require('example-package/src/Feature')

Now you can access it like this, 现在您可以像这样访问它,

import Feature from "example-package/Feature";

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

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