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

时间:2016-08-05 18:13:56

标签: node.js npm npm-install

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

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

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

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

由于一切都在NPM中,这可以以某种方式自动化吗?

1 个答案:

答案 0 :(得分:1)

你可以从包含模块的package.json中的postinstall钩子这样做:

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

这是npm postinstall上的一个很好的资源:

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