深入理解npm脚本


本文作者: jsweibo

本文链接: https://jsweibo.github.io/2019/05/05/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3npm%E8%84%9A%E6%9C%AC/

摘要

本文主要讲述了:

  1. 环境变量
  2. 特殊的 npm 脚本

正文

环境变量

npm run-script命令会把一些额外变量注入环境变量。

Path

示例:

1
2
3
4
5
6
7
{
"name": "learn_npm",
"version": "1.0.0",
"scripts": {
"dev": "node index.js"
}
}

learn_npm/index.js

1
console.log(process.env.Path);

运行:

1
2
3
#!/usr/bin/env bash

npm run-script dev

package.json

npm run-script命令会把package.json里的字段加上npm_package_前缀注入process.env对象中。

示例:

1
2
3
4
5
6
7
{
"name": "learn_npm",
"version": "1.0.0",
"scripts": {
"dev": "node index.js"
}
}

learn_npm/index.js

1
console.log(process.env.npm_package_name);

运行:

1
2
3
#!/usr/bin/env bash

npm run-script dev

输出:

1
learn_npm

npm 全局配置

npm run-script命令会把 npm 全局配置里的字段加上npm_config_前缀注入process.env对象中。

示例:

1
2
3
4
5
6
7
{
"name": "learn_npm",
"version": "1.0.0",
"scripts": {
"dev": "node index.js"
}
}

learn_npm/index.js

1
console.log(process.env.npm_config_registry);

运行:

1
2
3
#!/usr/bin/env bash

npm run-script dev

输出:

1
https://registry.npmmirror.com/

npm_lifecycle_event

npm run-script命令会把npm_lifecycle_event变量注入process.env对象中。

npm_lifecycle_event变量标志了当前执行的脚本名称。

示例:

1
2
3
4
5
6
7
{
"name": "learn_npm",
"version": "1.0.0",
"scripts": {
"dev": "node index.js"
}
}

learn_npm/index.js

1
console.log(process.env.npm_lifecycle_event);

运行:

1
2
3
#!/usr/bin/env bash

npm run-script dev

输出:

1
dev

特殊的 npm 脚本

env 脚本

env脚本是特殊的内建命令,默认用于罗列运行时中可用的环境变量。

如果开发者自己定义了env脚本,就以开发者定义的为准。

1
2
3
#!/usr/bin/env bash

npm run-script env

直接运行脚本

  • npm run-script start
  • npm run-script stop
  • npm run-script restart
  • npm run-script test

调用上面 4 个脚本的时候可以省略run-script,就像这样:

  • npm start
  • npm stop
  • npm restart
  • npm test

脚本前缀

脚本前缀一共有 2 个:

  • pre
  • post

示例:

1
2
3
4
5
6
7
8
9
{
"name": "learn_npm",
"version": "1.0.0",
"scripts": {
"predir": "echo 0",
"dir": "echo 1",
"postdir": "echo 2"
}
}

当开发者运行npm run-script dir时,npm 会依次执行:

  1. predir
  2. dir
  3. postdir

start 脚本

如果没有定义start脚本,且当前项目的根目录下存在server.js文件,则

1
2
3
#!/usr/bin/env bash

npm run-script start

1
2
3
#!/usr/bin/env bash

npm start

相当于

1
2
3
#!/usr/bin/env bash

node server.js

install 脚本

特别注意:npm install不仅会安装依赖,还会执行install脚本,而npm run-script install只会执行install脚本。

install脚本会在两个时候执行:

  • 包运行npm install的时候
  • 包被安装的时候(作为依赖)

install脚本的执行顺序:

  1. preinstall
  2. install
  3. postinstall

此外,install脚本还有默认行为:

如果没有定义install脚本或preinstall脚本,且当前项目的根目录下存在binding.gyp文件,则

1
2
3
#!/usr/bin/env bash

npm run-script install

1
2
3
#!/usr/bin/env bash

npm install

相当于

1
2
3
#!/usr/bin/env bash

node-gyp rebuild

uninstall 脚本

包被卸载的时候(作为依赖)会运行uninstall脚本。

uninstall脚本的执行顺序:

  1. preuninstall
  2. uninstall
  3. postuninstall

restart 脚本

如果没有定义restart脚本,则

1
2
3
#!/usr/bin/env bash

npm run-script restart

1
2
3
#!/usr/bin/env bash

npm restart

相当于依次执行:

  1. prerestart
  2. prestop
  3. stop
  4. poststop
  5. prestart
  6. start
  7. poststart
  8. postrestart

如果定义了restart脚本,则

1
2
3
#!/usr/bin/env bash

npm run-script restart

1
2
3
#!/usr/bin/env bash

npm restart

相当于依次执行:

  1. prerestart
  2. restart
  3. postrestart

参考资料

本文作者: jsweibo

本文链接: https://jsweibo.github.io/2019/05/05/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3npm%E8%84%9A%E6%9C%AC/


本文对你有帮助?请支持我


支付宝
微信