npx 快速执行 /node_modules/.bin 下的命令

  • 快速示例
  • 执行 gist
  • 执行 npm 命令
  • 编写 npm scripts 脚本

快速示例

使用 npx 执行 node_modules/.bin 下的命令

// npm 执行 bin 路径
➜  admin git:(master) npm bin
/Users/zhoukeke/workspace/gitlab/aliyun/admin/node_modules/.bin
// 查看 bin 路径有哪些命令
➜  admin git:(master) ls $(npm bin)
eslint  jade    riot    webpack
// 无法直接执行 bin 下的命令
➜  admin git:(master) riot
zsh: command not found: riot
// 通过 npx 成功执行
➜  admin git:(master) npx riot
	
Builds .tag files to .js

Options:

  -h, --help           You're reading it
  -v, --version        Print Riot's version
  -w, --watch          Watch for changes
  -c, --compact        Minify </p> <p> to </p><p>

快速执行 gist

如果因网络问题无法执行的,可参考给终端设置代理:https://zhoukekestar.github.io/notes/2018/04/06/set-proxy-iterm.html

➜  admin git:(master) npx https://gist.github.com/zkat/4bc19503fe9e9309e2bfaa2c58074d32
npx: installed 1 in 3.867s
yay gist
➜  admin git:(master)

快速执行 npm 命令

用 npx 执行一次性命令

➜  admin git:(master) cowsay wow
zsh: command not found: cowsay
// 在未安装 cowsay 的情况下,使用 npx 下载并执行 cowsay 命令
➜  admin git:(master) npx cowsay wow
npx: installed 10 in 11.303s
 _____
< wow >
 -----
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

编写 npm scripts 脚本

通过 npx 实现 npm scripts 的效果

➜  admin git:(master) npx -p cowsay -p lolcatjs -c 'echo "$npm_package_name@$npm_package_version" | cowsay | lolcatjs'

 ____________________
< xxx@3.5.0 >
 --------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

➜  admin git:(master)

References