npx
包与运行时
用法
npx [-y] <package>[@version] [args...]运行某个包的可执行文件,优先用 node_modules/.bin,找不到就临时下载;打错一个字就可能拉来陌生的包,加 --no 可以只用本地的。
常用选项
| 选项 | 含义 |
|---|---|
| -y | install without asking first |
| --no | fail instead of downloading anything |
| -p <pkg> | make a package available, then run a command from it |
| -c "<cmd>" | run the command through a shell with the bin path set |
| --package <pkg> | the long form of -p |
示例
npx tsc --noEmitruns the typescript in node_modules, no global install
npx -y create-next-app@latest my-appfetches a scaffolder for one use and forgets it
npx --no eslint .uses the local eslint or fails, instead of quietly downloading one
问题永远是什么装到哪里:装进这个项目,还是装到整台机器上。
怎么看
- 方括号 [ ] 表示这部分可以不写。
- 省略号 … 表示可以写多个。
- 选项区分大小写——有些命令里 -r 和 -R 不是一回事。
常见问题
Q. npx 是做什么的?
运行某个包的可执行文件,优先用 node_modules/.bin,找不到就临时下载;打错一个字就可能拉来陌生的包,加 --no 可以只用本地的。
Q. 怎么写?
npx [-y] <package>[@version] [args...] —— 方括号表示可以省略的部分。
Q. 值得记的选项有几个?
这里列了 5 个,完整列表在 man npx。这条命令属于包与运行时。
相关命令
man 手册: man npx