【问题标题】:`npm install` from sub directory of a Git repository来自 Git 存储库的子目录的“npm install”
【发布时间】:2019-08-29 19:16:09
【问题描述】:

您好,我已经创建了一个 Angular 库,并且我已经将它构建并推送到了 gitlab 的私有仓库中,现在我需要在另一个项目中使用它

所以我尝试使用 package.json 中的以下行导入它

"my-core-lib": "git+ssh://git@gitlab.com:<username>/my-core-lib.git"

但这是下载了整个 repo 并将其放在 node_modules 中,包括“dist”文件夹

我需要导入“dist”文件夹的内容,而不是整个仓库,例如:

"my-core-lib": "git+ssh://git@gitlab.com:<username>/my-core-lib.git/dist"

提前致谢

【问题讨论】:

  • 为什么不直接将它推送到 npm 注册表然后从那里安装呢?
  • @BrianRT 就我个人而言,我想避免一次性的错误修复和 PR 包弄乱我的命名空间(或者上帝禁止全局命名空间)

标签: angular git npm npm-install angular-library


【解决方案1】:

如果包源托管在 GitHub 上,您可以像这样使用GitPkg

# using npm:
npm install https://gitpkg.now.sh/<user>/<project>/<subdir>?<commit-ish>
# using yarn:
yarn add https://gitpkg.now.sh/<user>/<project>/<subdir>?<commit-ish>

在他们的site 上有一个很好的类似向导的表单,可以帮助构建 URL,以及安装它的命令。

【讨论】:

  • 哇,谢谢。 1) GitPkg 是救生员! 2) NPM 不支持这种开箱即用的功能令人非常失望。
【解决方案2】:

如果您构建了一个自定义库,并且入口点不在其 Git 存储库的根目录中,那么您只需要执行一些安装后处理 - 将您的源移动到正确的位置。

您还可以选择应用文件过滤器,以便npm 从存储库中复制您在模块中需要的文件/目录。

编辑package.json并添加两行:filesscripts -> postinstall,使文件看起来像这样:

{
  "name": "my-custom-angular",
  "version": "1.0.0",
  "main": "index.js",
  "files": ["dist"],
  "scripts": {
    "postinstall": "mv ./dist/* ./ && rmdir ./dist"
  }
}

GL!

【讨论】:

  • 注意 postinstall ...它确实工作得很好,但是如果你在开发库本身中添加一个包,它也会运行 postinstall 以及你在那里的任何命令。教训是艰难的……:D
猜你喜欢
  • 1970-01-01
  • 2018-05-01
  • 2016-01-06
  • 2012-05-06
  • 1970-01-01
  • 2015-05-15
  • 2016-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多