gpt4 book ai didi

node.js - 当对 npm 模块使用私有(private) git url 时,如何将使用应用程序配置为仅使用模块 dist 文件夹中的文件?

转载 作者:太空宇宙 更新时间:2023-11-03 23:25:53 25 4
gpt4 key购买 nike

我正在为我的 Angular 应用程序使用 Angular-CLI,但由于 Angular-CLI 目前不支持用于创建库,因此我使用看似最广泛使用的 git 项目来创建我的库:https://github.com/jvandemo/generator-angular2-library

我的问题是我不想将我的 npm 模块库发布到公共(public)目录。相反,我想直接在我的依赖项中使用 git url。在开发过程中,这工作得很好。我可以在本地运行构建并在“dist”文件夹中运行 npm 链接,一切都很顺利。但是,当我将 npm 模块代码推送到 git,然后在使用项目中运行 npm install 时,我不确定如何设置它,以便我的使用项目只查看模块的 dist 文件夹并将其视为模块的根。

例如,在 node_modules/my_private_module 中,我的文件结构如下所示:

dist/
-- component1
-- compoennt2
-- my_module.metadata.json
-- my_module.d.ts
-- my_module.umd.js
-- index.d.ts
-- index.js
-- package.json
-- README.MD
package.json
README.md

我的应用程序使用的所有文件都位于/dist 文件夹中,但我不想在所有导入中指定“dist”,例如

import { myComponent1 } from 'my_private_module/dist';

我希望能够指定

import { myComponent } from 'my_private_module";

正如我在开发过程中在 dist 文件夹中运行 npm 链接时所做的那样。

有什么办法可以实现这个目标吗?

谢谢

最佳答案

在模块的 package.json 中,在根文件夹中:

typings: 'dist/index.d.ts',
main: 'dist/index.js'

删除 dist 文件夹中的 package.json。

当从 import {...} from 'my_private_module' 解析包时,模块加载器将在 node_modules< 下查找名为 my_private_module 的文件夹,然后查找定义导出的 index.js ,或者在 package.json 中查找 main 属性 - 在您的情况下,该属性也指向 index.js从 dist 文件夹中。

最好将 package.json 放在您希望找到模块的位置,并使 maintypings 指向索引.js 和index.d.ts。

关于node.js - 当对 npm 模块使用私有(private) git url 时,如何将使用应用程序配置为仅使用模块 dist 文件夹中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44403561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com