npx
执行 npm 包中的命令,无需全局安装
语法
npx <command> [args]
参数
| 参数 | 说明 | 示例 | 级别 |
|---|---|---|---|
<command> |
执行包中的命令 | npx create-react-app my-app |
常用 |
-p <package> |
指定要使用的包 | npx -p typescript tsc --init |
进阶 |
--yes |
跳过确认提示 | npx --yes create-next-app |
常用 |
--no-install |
只使用已安装的包 | npx --no-install eslint . |
进阶 |
示例
创建 React 项目
npx create-react-app my-app
不需要全局安装 create-react-app
运行本地安装的工具
npx eslint src/
使用项目中安装的 eslint
临时使用某个版本
npx node@16 -e "console.log(process.version)"
临时用 Node 16 执行
初始化 TypeScript
npx tsc --init
生成 tsconfig.json
常见错误
command not found
包名可能不对,或网络问题无法下载
Need to install the following packages
正常提示,输入 y 确认安装即可
技巧
- npx 优先使用本地 node_modules/.bin 中的命令
- 用 npx 代替全局安装可以避免版本冲突
- npx 下载的包是临时的,用完即删