前言
前两天我准备用 create-react-app
创建一个新项目,然后我在命令行下执行
命令行下就会提示
提示意思是:create-react-app
从第五版本开始不再需要全局安装,让我先卸载 create-react-app
。
然后我就输入 npm uninstall -g create-react-app
进行全局卸载,然后再执行 npx create-react-app my-app
创建,结果还是上面的提示。
npx 介绍
npm 从 5.2 版开始,增加了 npx 命令。它有很多用处,主要使用有以下场景。
调用项目中的安装模块
原先要执行
代替
避免全局安装模块
上面代码运行时,npx 将create-react-app
下载到一个临时目录,使用以后再删除。
然后我去 google 搜索答案,找到了这个issue,上面回答了一些解决办法。
使用不同版本的 node
利用 npx
可以下载模块这个特点,可以指定某个版本的 Node 运行脚本。它的窍门就是使用 npm 的 node 模块。
上面命令会使用 0.12.8 版本的 Node 执行脚本。原理是从 npm 下载这个版本的 node,使用后再删掉。
某些场景下,这个方法用来切换 Node 版本,要比 nvm 那样的版本管理器方便一些。
执行 GitHub 源码
npx
还可以执行 GitHub 上面的模块源码。
执行 Gist 代码
执行仓库代码
注意,远程代码必须是一个模块,即必须包含package.json
和入口脚本
原因
产生这个问题的原因是 npx
是有缓存的,但全局卸载后,npx
的缓存还在。
解决办法
方案一 使用固定版本号
方案二
使用 npm init
代替
方案三
先清除 npx
缓存然后在初始化
以上就是本文全部内容,希望这篇文章对大家有所帮助,也可以参考我往期的文章或者在评论区交流你的想法和心得,欢迎一起探索前端。