Skip to content

Instantly share code, notes, and snippets.

@wulucxy
Last active November 7, 2018 14:43
Show Gist options
  • Save wulucxy/11a279a1b8984a5237e3bfca6f4c6010 to your computer and use it in GitHub Desktop.
Save wulucxy/11a279a1b8984a5237e3bfca6f4c6010 to your computer and use it in GitHub Desktop.
npm

首先,通过npm_package_前缀,npm 脚本可以拿到package.json里面的字段。比如,下面是一个package.json。

{ "name": "foo", "version": "1.2.5", "scripts": { "view": "node view.js" } } 那么,变量npm_package_name返回foo,变量npm_package_version返回1.2.5。

// view.js console.log(process.env.npm_package_name); // foo console.log(process.env.npm_package_version); // 1.2.5 上面代码中,我们通过环境变量process.env对象,拿到package.json的字段值。如果是 Bash 脚本,可以用$npm_package_name和$npm_package_version取到这两个值。

npm_package_前缀也支持嵌套的package.json字段。

"repository": { "type": "git", "url": "xxx" }, scripts: { "view": "echo $npm_package_repository_type" } 上面代码中,repository字段的type属性,可以通过npm_package_repository_type取到。

// npm 清理缓存

rm -rf node_modules package-lock.json && npm cache clean -f && wnpm i -d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment