从 git 中拉取模块后的 npm 运行 脚本

npm run script after pulling module from git

我有一个 Node 模块,其源代码位于 Git 存储库 (GitHub) 中。我可以使用 NPM 从 Git 将模块安装到 NPM 项目中:

npm install --save git@github.com:user/example.git

问题是在提取源代码后需要完成一些构建步骤。我不想在 Git 中包含构建结果,因为它们是构建步骤的产物,而不是真正的源代码。

目前,我需要手动移动到目录 (cd ./node_modules/example),然后 运行 构建脚本 (npm run build),但这很麻烦。

既然一切都在 NPM 中,这能以某种方式实现自动化吗?

您可以从 postinstall 挂钩中做到这一点,就像从包含模块的 package.json 中一样:

"scripts": {
    "postinstall": "cd ./node_modules/example && npm run build"
}

这里有一个关于 npm postinstall 的好资源:

http://krasimirtsonev.com/blog/article/Fun-playing-with-npm-dependencies-and-postinstall-script