首页 > 其他分享 >一个 create-react-app v5 的问题

一个 create-react-app v5 的问题

时间:2022-10-07 18:36:28浏览次数:39  
标签:npm app react v5 my create npx

前言

前两天我准备用 ​​create-react-app​​ 创建一个新项目,然后我在命令行下执行

npx create-react-app my-app

命令行下就会提示

Need to install the following packages:
create-react-app
Ok to proceed? (y) y

You are running `create-react-app` 4.0.3, which is behind the latest release (5.0.0).

We no longer support global installation of Create React App.

Please remove any global installs with one of the following commands:
- npm uninstall -g create-react-app
- yarn global remove create-react-app

The latest instructions for

提示意思是:​​create-react-app​​​ 从第五版本开始不再需要全局安装,让我先卸载 ​​create-react-app​​。

然后我就输入 ​​npm uninstall -g create-react-app​​​ 进行全局卸载,然后再执行 ​​npx create-react-app my-app​​ 创建,结果还是上面的提示。

npx 介绍

npm 从 5.2 版开始,增加了 npx 命令。它有很多用处,主要使用有以下场景。

调用项目中的安装模块

原先要执行

node-modules/.bin/jest

代替

npx jest

避免全局安装模块

npx create-react-app my-app

上面代码运行时,npx 将​​create-react-app​​下载到一个临时目录,使用以后再删除。

然后我去 google 搜索答案,找到了这个issue,上面回答了一些解决办法。

使用不同版本的 node

利用 ​​npx​​​ 可以下载模块这个特点,可以指定某个版本的 Node 运行脚本。它的窍门就是使用 npm 的  node 模块。

npx [email protected] -v

上面命令会使用 0.12.8 版本的 Node 执行脚本。原理是从 npm 下载这个版本的 node,使用后再删掉。

某些场景下,这个方法用来切换 Node 版本,要比 nvm 那样的版本管理器方便一些。

执行 GitHub 源码

​npx​​ 还可以执行 GitHub 上面的模块源码。

执行 Gist 代码

npx https://gist.github.com/zkat/4bc19503fe9e9309e2bfaa2c58074d32

执行仓库代码

npx github:piuccio/cowsay hello

注意,远程代码必须是一个模块,即必须包含​​package.json​​和入口脚本

原因

产生这个问题的原因是 ​​npx​​​ 是有缓存的,但全局卸载后,​​npx​​ 的缓存还在。

解决办法

方案一 使用固定版本号

npx create-react-app@5 <PROJECT_NAME>

方案二 使用 ​​npm init​​代替

npm init react-app my-app

方案三
先清除 ​​​npx​​ 缓存然后在初始化

npx clear-npx-cache
npx create-react-app my-app

以上就是本文全部内容,希望这篇文章对大家有所帮助,也可以参考我往期的文章或者在评论区交流你的想法和心得,欢迎一起探索前端。

标签:npm,app,react,v5,my,create,npx
From: https://blog.51cto.com/u_15757429/5734842

相关文章