create-react-app:找不到命令(React)错误[已解决]

目录

create-react-app: command not found (React) error [Solved]

  1. create-react-app:找不到命令
  2. ‘create-react-app’ 不被识别为内部或外部命令

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.

shell
# 👇️ 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.

If the global installation of create-react-app fails, you might have to run the command prefixed with sudo.
shell
# 👇️ 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.

If the error is not resolved, try restarting your terminal.

# Update your PATH on macOS or Linux

If that doesn’t help, run the following command:

shell
npm config get prefix

npm该命令将向您显示放置全局安装包的路径。全局包将位于bin指定路径的目录中。

查看操作系统上的 PATH 环境变量,npm config get prefix如果不存在则添加命令输出的路径。

如果将命令的输出添加到 PATH 环境变量,则必须重新启动所有打开的命令提示符才能生效。

如果这不起作用,请尝试将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文件中。

~/.bashrc
# 👇️ make sure to update the path with the output # from the command export PATH="/usr/local/share/npm/bin:$PATH"
如果将命令的输出添加到 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.

During the installation, you might get a prompt for whether you want to automatically update the PATH environment variable on your system, make sure to tick the option.
shell
# 👇️ 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
If the global installation of create-react-app fails, you might have to run the command prefixed with sudo.
shell
# 👇️ 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.

If the global installation of create-react-app fails, you have to open your shell as an administrator or run the command prefixed with sudo.
shell
# 👇️ 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.

If the error is not resolved, try restarting your terminal.

# Update your PATH on Windows

If that didn’t help, run the following command:

shell
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.

如果将命令的输出添加到 PATH 环境变量,则必须重新启动所有打开的命令提示符才能生效。

在 Windows 上,该npm config get prefix命令的输出类似于:C:\Users\Your_User_Name\AppData\Roaming\npm

要在 Windows 机器上更新 PATH,您必须:

  1. 打开开始搜索并输入env然后点击“编辑系统环境变量”
  2. 然后点击“环境变量”
  3. 编辑Path变量并添加从命令获得的输出
    npm config get prefix

路径应该看起来像C:\Users\Your_User_Name\AppData\Roaming\npm(确保Your_User_name用您的实际用户名替换占位符)。

如果将命令的输出添加到 PATH 环境变量,则必须重新启动所有打开的命令提示符才能生效。

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.

shell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Make sure to open your PowerShell as an administrator before you run the 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.

During the installation, you might get a prompt for whether you want to automatically update the PATH environment variable on your system, make sure to tick the option.
shell
# 👇️ 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
If the global installation of create-react-app fails, you might have to open your shell as an administrator or run the command prefixed with sudo.
shell
# 👇️ 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.