gpt4 book ai didi

npm - 从本地目录加载 npm 包,无需复制不必要的文件/文件夹,例如 node_modules

转载 作者:行者123 更新时间:2023-12-04 14:06:23 25 4
gpt4 key购买 nike

假设我必须开发一个 npm 包,我的包裹 , 需要由 导入使用我的项目 .

开发中我的包裹我需要从 npm 安装和使用其他软件包。开发末我的包裹 ,我把所有相关文件都捆绑在了dist目录下。由于我不想将 node_modules 文件夹中的所有文件作为发布包的一部分,我在 .gitignore(或 .npmignore)中指定排除 node_modules 文件夹。所以文件夹的最终结构我的包裹看起来像

my-package
dist
... // all the code of the package to be distributed
node_modules
... // all the node modules
src
... // all source files
package.json
.gitignore

现在我发布 我的包裹在 npm 存储库中,然后将其导入 我的项目 ,使用命令 npm i my-package .

由于这种导入,目录托管 的结构我的项目 就像
my-project
... // stuff
node_modules
my-package
dist
... // all the code of the package to be distributed
src
... // all source files
package.json

正如预期的那样,在 下没有导入 node_modules 文件夹我的包裹文件夹。

由于我是两者的开发者 我的包裹我的项目 , 在开发过程中我想导入 我的包裹从本地路径使用一个命令,看起来像
npm -i ../my-package
如果我这样继续,我看到的结果是所有文件和文件夹都存储在 下。我的包裹目录复制到 的node_modules文件夹下我的项目 ,即不考虑 .gitignore(或 .npmignore)排除项。最终结果是一个结构,如
my-project
... // stuff
node_modules
my-package
dist
... // all the code of the package to be distributed
node_modules
... // all stuff imported by my-package
src
... // all source files
package.json
... // any other file under my-package

有没有办法只从本地路径安装 的相关文件我的包裹如果软件包是从 npm 存储库安装的,会发生这种情况吗?

最佳答案

我们今天遇到了同样的问题。我们通过添加到本地包的package.json中来搞定

“build”: “yarn run clean && yarn build-components && yarn pack”

然后到使用我们添加到package.json的依赖项的本地包的项目
“yourpackage”: “file:../your-tgz.tgz”,

希望能帮助到你

注意:如果您使用 yarn ,您可能会遇到缓存问题。

关于npm - 从本地目录加载 npm 包,无需复制不必要的文件/文件夹,例如 node_modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48458305/

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