目录
create-react-app: command not found (React) error [Solved]
create-react-app: 命令未找到
用于npx
解决错误“create-react-app:找不到命令”,例如
npx create-react-app my-app
或通过运行全局安装包
npm install -g create-react-app
以便能够使用不带
npx
前缀的命令。
解决错误的最快方法是使用命令npx
。
# 👇️ clear the npx cache npx clear-npx-cache # 👇️ for normal React.js project npx create-react-app my-app # 👇️ for TypeScript React.js project npx create-react-app my-app --template typescript
如果错误未解决,您可以强制命令使用最新版本的create-react-app
.
# 👇️ for normal React.js project npx create-react-app@latest my-app # 👇️ for TypeScript React.js project npx create-react-app@latest my-app --template typescript
The commands above use the latest version of
create-react-app to start a new
project.
# Installing create-react-app
globally
Alternatively, you can install create-react-app globally by running the
following command.
# 👇️ install create-react-app globally npm install -g create-react-app@latest # 👇️ for normal React.js project create-react-app my-app # 👇️ for TypeScript React.js project create-react-app my-app --template typescript
Now you are able to use the create-react-app
command without having to prefix
it with npx
.
create-react-app
fails, you might have to run the command prefixed with sudo
.# 👇️ if you got a permissions error sudo npm install -g create-react-app@latest # 👇️ for normal React.js project create-react-app my-app # 👇️ for TypeScript React.js project create-react-app my-app --template typescript
If you are able to run the create-react-app --version
command and get the
version number of the package, then the installation has succeeded.
# Update your PATH on macOS or Linux
If that doesn’t help, run the following command:
npm config get prefix
npm
该命令将向您显示放置全局安装包的路径。全局包将位于bin
指定路径的目录中。
查看操作系统上的 PATH 环境变量,npm config get prefix
如果不存在则添加命令输出的路径。
如果这不起作用,请尝试将bin
文件夹的路径 (from
npm config get prefix
) 添加到您的 PATH 环境变量并重新启动您的终端。
例如,在 macOS 上,您可以使用以下命令更新您的路径:
# make sure path matches with npm config get prefix export PATH=/usr/local/share/npm/bin:$PATH
如果您使用的是 Linux,则可以将命令的输出添加npm config get prefix
到您的.bashrc
文件中。
# 👇️ make sure to update the path with the output # from the command export PATH="/usr/local/share/npm/bin:$PATH"
# Reinstalling Node.js
If that doesn’t help try to reinstall Node.js on your machine and then install
create-react-app
globally by running npm install -g create-react-app@latest
.
# 👇️ install create-react-app globally npm install -g create-react-app@latest # 👇️ for normal React.js project create-react-app my-app # 👇️ for TypeScript React.js project create-react-app my-app --template typescript
create-react-app
fails, you might have to run the command prefixed with sudo
.# 👇️ install create-react-app globally sudo npm install -g create-react-app@latest # 👇️ for normal React.js project create-react-app my-app # 👇️ for TypeScript React.js project create-react-app my-app --template typescript
Alternatively, you can see how you can fix the permissions error on this page in
the
official npm docs.
# ‘create-react-app’ is not recognized as an internal or external command
用于npx
解决错误“create-react-app 未被识别为内部或外部命令、可操作程序或批处理文件”,例如
npx create-react-app my-app
或通过运行 全局安装包
npm install -g create-react-app
。
解决错误的最快方法是使用命令npx
。
# 👇️ clear the npx cache npx clear-npx-cache # 👇️ for normal React.js project npx create-react-app my-app # 👇️ for TypeScript React.js project npx create-react-app my-app --template typescript
如果错误未解决,您可以强制命令使用最新版本的create-react-app
.
# 👇️ for normal React.js project npx create-react-app@latest my-app # 👇️ for TypeScript React.js project npx create-react-app@latest my-app --template typescript
上面的命令使用最新版本的
create-react-app来启动一个新项目。
全局安装 create-react-app
或者,您可以通过运行以下命令来全局安装 create-react-app。
# 👇️ install create-react-app globally npm install -g create-react-app@latest # 👇️ for normal React.js project create-react-app my-app # 👇️ for TypeScript React.js project create-react-app my-app --template typescript
现在您可以使用该create-react-app
命令,而无需在其前面添加npx
.
create-react-app
fails, you have to open your shell as an administrator or run the command prefixed with sudo
.# 👇️ if you got a permissions error sudo npm install -g create-react-app@latest # 👇️ for normal React.js project create-react-app my-app # 👇️ for TypeScript React.js project create-react-app my-app --template typescript
If you are able to run the create-react-app --version
command and get the
version number of the package, then the installation has succeeded.
# Update your PATH on Windows
If that didn’t help, run the following command:
npm config get prefix
The command will show you the path where npm
puts your globally installed
packages. The global packages will be in the bin
directory at the specified
path.
Look at the PATH environment variable on your operating system and add the
path that the npm config get prefix
command outputs if it’s not already
there.
在 Windows 上,该npm config get prefix
命令的输出类似于:C:\Users\Your_User_Name\AppData\Roaming\npm
。
要在 Windows 机器上更新 PATH,您必须:
- 打开开始搜索并输入
env
然后点击“编辑系统环境变量” - 然后点击“环境变量”
- 编辑
Path
变量并添加从命令获得的输出
npm config get prefix
。
路径应该看起来像C:\Users\Your_User_Name\AppData\Roaming\npm
(确保Your_User_name
用您的实际用户名替换占位符)。
If you get the error “create-react-app cannot be loaded because running
scripts is disabled on this system”, open your PowerShell as an administrator
and set its execution policy with the
Set-ExecutionPolicy
command.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Set-ExecutionPolicy
command.This effectively removes the execution policy of Restricted
, which doesn’t
allow us to load configuration files or run scripts. The Restricted
execution
policy is the default for Windows client computers.
If you add the output from the command to your PATH environment variable, you
have to restart any open command prompts before it takes effect.
# Reinstalling Node.js
If that doesn’t help try to reinstall Node.js on your
machine and then install create-react-app
globally by running
npm install -g create-react-app
.
# 👇️ install create-react-app globally npm install -g create-react-app@latest # 👇️ for normal React.js project create-react-app my-app # 👇️ for TypeScript React.js project create-react-app my-app --template typescript
create-react-app
fails, you might have to open your shell as an administrator or run the command prefixed with sudo
.# 👇️ if you got permissions error sudo npm install -g create-react-app@latest # 👇️ for normal React.js project create-react-app my-app # 👇️ for TypeScript React.js project create-react-app my-app --template typescript
Alternatively, you can see how you can fix the permissions error on this page in
the
official npm docs.