深入理解npm run命令,run命令的两条潜规则

在本文中,苏南大叔描述的就是npm run的潜规则:pre和post。而且在查看本文的内容前,您一定要知晓:npm run命令都是定义在package.json文件的scripts节点里面的。测试环境:mac,[email protected]

{
  "name": "nnn",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "echo \"test\"",
    "predev": "echo \"before dev\"",
    "dev": "echo \"deving\"",
    "postdev": "echo \"after dev\""
  },
  "author": "sunan",
  "license": "ISC"
}

潜规则一:npm start和npm run start

对于命令npm run ***,如果***是start字样,就可以省略run字样。即下面两条命令是等同的

npm run start
npm  start

潜规则二:pre和post
对于某个命令***,如果定义:

pre***,那么就会在npm run 之前运行。
pos
,那么就会在npm run ***之后运行。

例:

"scripts": {
    "start": "echo \"test\"",
    "predev": "echo \"before dev\"",
    "dev": "echo \"deving\"",
    "postdev": "echo \"after dev\""
  },

执行 npm run dev 后会输出:

before dev
deving
after dev

你可能感兴趣的:(前端,npm,node.js)