请教一下怎样在npm install 之后执行npm scripts里面的内容?
发布于 7 年前 作者 fernandou 6068 次浏览 来自 问答

如题,看到很多开源项目都是在npm install可以启动项目,所以想知道一下怎么实现的?

6 回复

在package.json文件中,  再scripts中写你要执行的文件

"scripts": {
    "start": "node ./bin/http-server"
  }

npm scripts有两种钩子:

  • pre 前置 prestart
  • post 后置 poststart

一般是执行某个命令的时候将npm install作为前置, 比如:

...
"scripts": {
	"prebuild": "npm install",
	"build": "webpack"
}

那么你在执行 npm run build的时候,npm会先去执行npm install,然后再执行build

当然,你也可以npm install之后干点别的, 比如:

"scripts": {
	"postinstall": "node bin/check_logs_dir.js"
}

这样就能在执行脚本之后做别的事。

@luoyjx 好的,十分感谢

@luoyjx 还有这种前后置的钩子啊 这个不错 孤陋寡闻啦

@hezhongfeng @fernandou 更加详细的,可以看看阮一峰老师的这篇 npm scripts 使用指南

@luoyjx 好的 顺便问一哈node --harmony-async-await 这种和谐模式在哪里有说明?我在node的doc里面没找到,只知道这种和谐模式可以使用async了。

回到顶部