未使用 create-react-app 提供模板

Sam*_*gNY 398 reactjs create-react-app

当我create-react-app my-app在终端中键入命令时,它似乎可以工作 - 成功下载所有库等。但是在该过程结束时,我收到一条消息,显示template was not provided.

输入

user@users-MacBook-Pro-2 Desktop% create-react-app my-app
Run Code Online (Sandbox Code Playgroud)

输出

Creating a new React app in /Users/user/Desktop/my-app.

Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts...
..... nothing out of the ordinary here .....
?  Done in 27.28s.

A template was not provided. This is likely because you're using an outdated version of create-react-app.
Please note that global installs of create-react-app are no longer supported.
Run Code Online (Sandbox Code Playgroud)

在 package.json 中my-app

"dependencies": {
  "react": "^16.12.0",
  "react-dom": "^16.12.0",
  "react-scripts": "3.3.0" <-- up-to-date
}
Run Code Online (Sandbox Code Playgroud)

我查看了 CRA更改日志,看起来好像添加了对自定义模板的支持 - 但看起来命令create-react-app my-app不会改变。

知道这里发生了什么吗?

小智 542

如果您之前已create-react-app通过 全局安装npm install -g create-react-app,我们建议您卸载软件包npm uninstall -g create-react-app以确保npx始终使用最新版本。

文档

使用以下任一命令:

  • npx create-react-app my-app
  • npm init react-app my-app
  • yarn create react-app my-app

如果npm uninstall -g create-react-app上述说明不起作用。 键入which create-react-app以了解其安装位置。我的安装在/usr/bin文件夹中。然后做sudo rm -rf /usr/bin/create-react-app。(归功于下面的@v42 评论)

  • 如果在 `npm uninstall -g create-react-app` 之后仍然出现相同的错误,请在控制台中输入 `which create-react-app`。如果它返回类似 `/usr/local/bin/create-react-app` 的内容,则执行 `rm -rf /usr/local/bin/create-react-app` 手动删除。 (159认同)
  • 我认为 npm uninstall -g create-react-app 和 npx create-react-app my-app 可以工作。但不知何故,npx 仍然创建了一个没有模板的项目。create-react-app 是否以某种方式缓存?我认为全局 cra 安装将被弃用。在使用 npx 之前,我必须更新 create-react-app -g 。 (24认同)
  • 恕我直言,这不是一个好方法。为什么我们每次都必须下载并运行东西而不是拥有本地副本?它浪费带宽,也是一种从互联网执行随机代码的好方法,有一天会被黑客攻击......只需像我那样重新安装它“npm install create-react-app -g”我的解决方案意味着你最终会得到这个再次出错,但人们*不应该*从网络上运行任意代码,并且如果他们选择的话应该能够保留在特定版本上...... (14认同)
  • 终于让它适用于打字稿模板 `npm uninstall -g create-react-app` `sudo rm -rf /usr/local/bin/create-react-app` `npx create-react-app newapp --template typescript` (9认同)
  • 如果这些解决方案对您不起作用,您可能像我一样通过“yarn”全局安装了该软件包。在这种情况下,请使用“yarn global remove create-react-app”将其删除。 (6认同)
  • 使用此“npm list -g --depth=0”来查看 create-react-app 是否已在已安装的节点包中列出。如果是,请运行“npm uninstall -g create-react-app”进行卸载。 (2认同)

Rob*_*uch 84

1)

npm uninstall -g create-react-app
Run Code Online (Sandbox Code Playgroud)

或者

yarn global remove create-react-app
Run Code Online (Sandbox Code Playgroud)

2)

似乎存在一个错误,即 create-react-app 未正确卸载并使用新命令之一导致:

未提供模板。这可能是因为您使用的是过时版本的 create-react-app。

使用 卸载它后npm uninstall -g create-react-app,检查您的命令行上是否仍然使用which create-react-app(Windows: where create-react-app) “安装”了它。如果它返回一些东西(例如/usr/local/bin/create-react-app),然后rm -rf /usr/local/bin/create-react-app手动删除。

3)

然后这些方法之一:

npx create-react-app my-app
npm init react-app my-app
yarn create react-app my-app
Run Code Online (Sandbox Code Playgroud)

  • 我必须删除安装文件夹, npm uninstall create-react-app -g 不起作用 (5认同)
  • $ echo '#!/bin/sh' &gt; /usr/local/bin/create-react-app $ echo 'echo 使用纱线 create-react-app 代替' &gt; /usr/local/bin/create-react-app $ chmod 0700 /usr/local/bin/create-react-app (2认同)
  • 在 Windows 上,您可以使用命令“where”而不是“which” (2认同)

小智 50

虽然已经有很多答案在这里。我想出了 3 个解决方案,当我遇到这种情况时,我一步一步地应用了这些解决方案。


第一步:从官方手册,

如果您之前通过 全局安装了 create-react-app npm install -g create-react-app,我们建议您卸载软件包npm uninstall -g create-react-app以确保 npx 始终使用最新版本。

https://create-react-app.dev/docs/getting-started

您可以在下面使用这些命令:

  • npx create-react-app my-app
  • npm init react-app my-app
  • yarn create react-app my-app

第二步(如果第一个不起作用):

有时它可能会保留缓存。然后您可以使用下面给出的这些命令。

  • npm uninstall -g create-react-app
  • npm cache clean --force
  • npm cache verify
  • yarn create react-app my-app

第三步:(如果这两个不起作用)首先通过 卸载npm uninstall -g create-react-app,然后检查您是否仍然使用which create-react-app命令行上的命令“安装”了它。如果你有类似(/usr/local/bin/create-react-app)的东西,那么运行这个rm -rf /usr/local/bin/create-react-app(文件夹可能会有所不同)手动删除。然后再次通过 npx/npm/yarn 安装它。

注意:我在最后一步成功了。


小智 34

首先通过以下命令全局卸载 create-react-app:

npm uninstall -g create-react-app
Run Code Online (Sandbox Code Playgroud)

然后在您的项目目录中:

npm install create-react-app@latest
Run Code Online (Sandbox Code Playgroud)

最后:

npx create-react-app my-app
Run Code Online (Sandbox Code Playgroud)


Bak*_*rov 21

将更多内容添加到上述答案中:

使用新版本的create-react-app,您可以使用自定义模板创建新应用程序。目前可用的两个模板:

  • cra-模板
  • cra-模板打字稿

用法

npx create-react-app my-app [--template typescript]
Run Code Online (Sandbox Code Playgroud)

有关最新更改的更多详细信息create-react-app

https://github.com/facebook/create-react-app/releases/tag/v3.3.0


小智 19

我也有同样的问题。当我尝试npm init react-app my-app命令返回相同的消息时

未提供模板。这可能是因为您使用的是过时版本的 create-react-app。

yarn create react-app my-app 命令工作正常。


met*_*Dev 18

这对我有用。

  1. npm uninstall -g create-react-app
  2. npx create-react-app my-app


小智 17

  1. npm install -g create-react-app 在你的电脑里
  2. 再次创建反应项目 npx create-react-app my-app

  • 不过,建议全局卸载 create-react-app 。 (10认同)

Dev*_*mUK 15

首先清除你的 npm 缓存,然后使用 yarn 如下:

  • npm cache clean --force
  • npm cache verify
  • yarn create react-app my-app

我希望这有帮助。

编辑

...在我进一步研究此问题后,您可能想尝试以下操作:

  1. npm uninstall -g create-react-app
  2. yarn global remove create-react-app
  3. which create-react-app - 如果它返回一些东西(例如 /usr/local/bin/create-react-app),然后执行 rm -rf /usr/local/bin/create-react-app 手动删除。
  4. npm cache clean --force
  5. npm cache verify
  6. npx create-react-app@latest

这些步骤应该删除全局安装的 create-react-app 安装,然后手动删除链接到旧的全局安装的 create-react-app 脚本的旧目录。清除 npm 缓存以确保不使用任何旧缓存版本的 create-react-app 是一个好主意。最后,创建与新reactjs的应用程序@latest,像这样的选择:npx create-react-app@latest。在使用 npx create-react-app 时没有创建模板的这个问题有很多混乱,如果你按照我上面提到的步骤(1-6)那么我希望你能成功。

ps

如果我想在名为 client 的目录中创建一个 react 应用程序,那么我将在终端中键入以下命令:

npx create-react-app@latest ./client

祝你好运。


小智 13

这两个步骤对我有用

1)使用此命令全局卸载react-app

npm uninstall -g create-react-app
Run Code Online (Sandbox Code Playgroud)

2) 使用此命令在项目文件夹中安装 react-app

npx create-react-app project-name
Run Code Online (Sandbox Code Playgroud)


小智 13

所以我已经完成了这里的所有步骤,但没有帮助。

TLDR;跑npx --ignore-existing create-react-app

我在 Mac 上 Mojave 10.15.2

CRA不是全球的装机量-在没有找到它/usr/local/lib/node_modules/usr/local/bin任何。

然后我在 CRA 的 github 问题上看到了这个评论。运行带有--ignore-existing标志的命令有帮助。


Buk*_*Lau 11

npx create-react-app@latest your-project-name

在尝试了所有答案后为我工作,希望可以在将来帮助某人。


Gul*_*ibi 10

"如果您之前已经通过 npm install -g create-react-app 全局安装了 create-react-app,我们建议您使用 npm uninstall -g create-react-app 卸载包,以确保 npx 始终使用最新版本"

这在https://create-react-app.dev/docs/getting-started/报告。对我来说,这不起作用。我不得不在全局重新安装 create-react-app。

我解决这个问题的步骤是:

  1. npm uninstall -g create-react-app
  2. npm install -g create-react-app
  3. npx create-react-app my-app

  • 这是事实,但是,我无法找到在不进行全局安装的情况下安装模板的另一种方法。无论如何,这对我的应用程序没有什么影响。 (4认同)

小智 10

在 MacOS 上,所有选项都不适用于我。有效的是以下 3 个步骤:

  1. 从以下位置删除节点:/usr/local/bin/

然后

  1. 新安装节点:https : //nodejs.org/en/

然后

  1. 安装 react:npx create-react-app my-app 关注:https : //create-react-app.dev/


cod*_*ver 7

对于 Linux,这对我有用

sudo npm uninstall -g create-react-app
npx create-react-app my-test-app
Run Code Online (Sandbox Code Playgroud)


JBa*_*lin 7

TLDR:npm uninstall -g create-react-app使用npx create-react-app app.


问题

您使用的是create-react-app使用 npm 全局安装的旧版本。该create-react-app命令调用此全局包。

您可以通过运行npm outdated -g create-react-appcreate-react-app --versionnpm view create-react-app.

的版本react-scripts是最新的这一事实与引导应用程序的包的版本无关 ( create-react-app),后者获取它使用的包的最新版本(react-scripts在这种情况下)。

解决方案

如果要继续使用该create-react-app命令,则需要使用npm update -g create-react-app. 请注意,您需要定期执行此操作以使其保持最新状态。您会注意到create-react-app不建议这样做(在安装日志中注明)。

更好的方法是完全删除全局安装 ( npm uninstall -g create-react-app) 并改为使用,npx以便它每次都获取最新版本的包(更多详细信息见npx下文)。

您应该通过尝试使用create-react-app以确保“未找到”命令来确认它已被全局卸载。

卸载问题?

您可以使用which create-react-app. 如果您在卸载它时遇到问题,您的机器上可能有多个 node/npm 版本(来自多次安装,或者因为您使用了 node 版本管理器,例如nvm)。这是一个单独的问题,我不会在这里解决,但在这个答案中有一些信息。

一个快速的核方法是rm -rfwhich create-react-app返回的路径上强行移除它 ( ) 。


补充

全局 npm 包和npx命令

$ NPM_PACKAGE_NAME 无论您在哪个目录中,都将始终使用包的全局安装版本。

$ npx NPM_PACKAGE_NAME 将使用从当前目录搜索到根目录时找到的第一个版本的包:

  • 如果您的当前目录中有该软件包,它将使用该软件包。
  • 否则,如果您的包位于当前目录的父目录中,它将使用它找到的第一个。
  • 否则,如果您全局安装了该软件包,它将使用该软件包。
  • 否则如果你根本没有这个包,它会暂时安装它,使用它,然后丢弃它。-这是确保包是最新的最好方法

可以在此答案中找到有关 npx 的更多信息。

使用npxcreate-react-app

create-react-app有一些特殊的命令/别名来创建npx特定于该包 ( yarn create react-app, npm init react-app)的 react 应用程序(而不是),但npx create-react-app与其他包的工作方式相同。

yarnnpm全球安装

Yarn 将全局安装存储在与 不同的文件夹中npm,这就是为什么yarn create react-app可以在不卸载全局 npm 包的情况下立即工作(就 Yarn 而言,该包尚未安装)。

不过,这只是一个临时解决方案,因为您需要记住在使用 Create React App 时始终使用 yarn 而不是 npm。


小智 5

这解决了我的问题

脚步:

1.卸载create-react应用

npm uninstall -g create-react-app
Run Code Online (Sandbox Code Playgroud)

2.现在只需使用

npx create-react-app my-app
Run Code Online (Sandbox Code Playgroud)

这将自动为 u 创建模板。


Lah*_*nge 5

这对我有用!

1) npm uninstall -g create-react-app

2) npm install -g create-react-app

3) npx create-react-app app_name

如果您以前create-react-app通过 全局安装了任何npm install -g create-react-app,最好使用卸载它npm uninstall -g create-react-app


Lui*_*ses 5

这个问题不是这样解决的,问题出在不同的node实例上,尝试全局删除create-react-app,然后从root用户中删除node_modules和package-lock.json