简体   繁体   English

如何在整个项目中将自定义模块导出为全局模块,并通过仅提供名称而不是 js 中的路径来导入?

[英]How to export custom modules as global across the project and import by just providing the name rather than path in js?

I have this how do you make your custom module global accross you project and import it by providing the name of the module rather than the path like the modules in npm ecosystem我知道如何通过提供模块的名称而不是像 npm 生态系统中的模块那样的路径来使您的自定义模块在您的项目中成为全局模块并导入它

import CustomModule from "custom-modules";

After looking for a while i found the answer.寻找了一段时间后,我找到了答案。 You have to add your exports in package.json in order to use them with the name or path provided in package.json.您必须在 package.json 中添加导出,以便将它们与 package.json 中提供的名称或路径一起使用。 eg例如

 {
  "name": "my-mod",
  "exports": {
    ".": "./lib/index.js",
    "./lib": "./lib/index.js",
    "./lib/index": "./lib/index.js",
    "./lib/index.js": "./lib/index.js",
    "./feature": "./feature/index.js",
    "./feature/index.js": "./feature/index.js",
    "./package.json": "./package.json"
  }
}

You can go Here for more details你可以go这里了解更多详情

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

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