NodeJS之npm run xxx 过程

NodeJS

比如如下的: package.json, 执行 npm run dev

{
  "scripts": {
    "dev": "vuepress",
    "pretest": "node pretest.js",
    "test": "node test.js",
    "posttest": "node posttest.js"
  }
}
1
2
3
4
5
6
7
8
  1. 执行 npm run dev,读取package.json 的scripts 对应的脚本命令(dev:vuepress),vuepress是个可执行脚本,查找规则是:
  • 先从当前项目node_modules/.bin去查找可执行命令vuepress
  • 如果没找到就去全局node_modules 去找可执行命令vuepress
  • 如果还没找到就去环境变量查找
  • 再找不到就进行报错

如果成功找到会发现有三个文件:

- node_modules/.bin/vuepress // sh 文件
- node_modules/vite/bin/vuepress.CMD // cmd 文件
- node_modules/vite/bin/vuepress.ps1 // ps1 文件
1
2
3

因为nodejs 是跨平台的所以可执行命令兼容各个平台

  • .sh文件是给Linux unix Macos 使用
  • .CMD 给windows的cmd使用
  • .ps1 给windows的powerShell 使用
  1. 如果 执行 npm run test, 那么会依次执行:
node pretest.js
node test.js
node posttest.js
1
2
3

执行 npm run test 命令的时候 pretest 会自动先执行,他的生命周期是在 test之前,然后执行test 命令,然后最后执行 posttest

npm 执行命令也有生命周期 (opens new window),具体可以参考官网 (opens new window)

{
  "scripts": {
    "prexxx": "最先执行",
    "xxx": "再执行",
    "postxxx": "最后执行"
  }
}
1
2
3
4
5
6
7

例如:

// pretest.js 内容:
console.log('pre test')

// test.js  内容:
console.log('test')

// posttest.js  内容:
console.log('post test')

// package.json 内容:
`{
  "name": "node_test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "pretest": "node pretest.js",
    "test": "node test.js",
    "posttest": "node posttest.js",

    "prexxx": "node pretest.js",
    "xxx": "node test.js",
    "postxxx": "node posttest.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}`

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

执行:npm run test 或者 npm run xxx 结果:

"C:\Program Files\nodejs\npm.cmd" run xxx

> node_test@1.0.0 prexxx
> node pretest.js

pre test

> node_test@1.0.0 xxx
> node test.js

test

> node_test@1.0.0 postxxx
> node posttest.js

post test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17