【问题标题】:Template not provided using create-react-app未使用 create-react-app 提供模板
【发布时间】:2020-03-29 23:59:50
【问题描述】:

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

输入

user@users-MacBook-Pro-2 Desktop% create-react-app my-app

输出

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.

my-app的package.json中:

"dependencies": {
  "react": "^16.12.0",
  "react-dom": "^16.12.0",
  "react-scripts": "3.3.0" <-- up-to-date
}

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

知道这里发生了什么吗?

【问题讨论】:

  • 传闻全球包已经不那么火了。尝试'npm init react-app my-app'
  • 同时更新您的安装
  • 谢谢 - 最终工作(使用 Yarn)正在升级 CRA:yarn global upgrade create-react-app - 然后做 yarn create react-app my-app
  • 我在 Windows 机器上遇到了同样的问题
  • 这里也一样,在 Windows 上 npm uninstall -g create-react-app 没有解决它。我必须去文件夹C:\Users\serge\AppData\Roaming\npm(你的路径会有所不同),并在这个文件夹中执行npm uninstall creat-react-app。之后我安装了npx create-react-app sample-react --template typescript --use-npm

标签: reactjs create-react-app


【解决方案1】:
  1. npm install -g create-react-app 在你的电脑里
  2. 使用npx create-react-app my-app 再次创建反应项目

【讨论】:

  • 建议全局卸载create-react-app。
  • 使用此方法有新更新时会返回错误
  • 我认为这是导致错误首先在全球安装 create-react-app
【解决方案2】:

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

Docs

使用以下任一命令:

  • 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 和 npx create-react-app my-app 会起作用。但不知何故,npx 仍然创建了一个没有模板的项目。 create-react-app 是否以某种方式缓存?我认为不推荐使用全球 cra 安装。在使用 npx 之前,我必须更新 create-react-app -g。
  • 如果在npm uninstall -g create-react-app 之后仍然出现同样的错误,请在控制台中输入which create-react-app。如果它返回类似/usr/local/bin/create-react-app 的内容,则执行rm -rf /usr/local/bin/create-react-app 手动删除。
  • 恕我直言,这不是一个好方法。为什么我们每次都必须下载和运行东西而不是本地副本?它浪费带宽和从互联网上执行随机代码的一种非常好的方法,有一天会被黑客入侵......就像我一样重新安装它npm install create-react-app -g我的解决方案意味着你最终会再次遇到这个错误但是人们应该不会从网络上运行任意代码,如果他们选择这样做,应该能够保持在特定版本上......
  • 终于让它适用于打字稿模板npm uninstall -g create-react-appsudo rm -rf /usr/local/bin/create-react-appnpx create-react-app newapp --template typescript
  • 如果这些解决方案不适合您,您可能像我一样通过yarn 全局安装了该软件包。在这种情况下,使用yarn global remove create-react-app 将其删除。
【解决方案3】:

要在上面的答案中添加更多内容:

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

  • cra 模板
  • cra-template-typescript

用法

npx create-react-app my-app [--template typescript]

create-react-app的最新变化的更多细节:

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

【讨论】:

    【解决方案4】:

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

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

    但是

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

    【讨论】:

      【解决方案5】:

      这对我有用。

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

      【讨论】:

        【解决方案6】:

        使用此命令后:

        yarn global upgrade create-react-app
        

        然后我尝试了:

        yarn create-react-app my-app but it didn't work for me.
        

        这很有效:

        npx create-react-app my-app 
        

        【讨论】:

          【解决方案7】:

          "如果您之前通过 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 卸载 -g create-react-app
          2. npm install -g create-react-app
          3. npx create-react-app my-app

          【讨论】:

          • 不建议再使用全局安装create-react-app了。
          • 这是真的,但是,如果不进行全局安装,我想不出另一种安装模板的方法。无论如何,这对我的应用没有任何影响。
          【解决方案8】:

          这解决了我的问题

          步骤:

          1.卸载create-react应用

          npm uninstall -g create-react-app
          

          2.现在只需使用

          npx create-react-app my-app
          

          这会自动为你创建模板。

          【讨论】:

            【解决方案9】:

            这是一个奇怪的问题,因为昨天这对我有用,今天早上我遇到了同样的错误。根据发行说明,添加了一项新功能以支持模板,因此命令行中的一些部分似乎发生了变化(例如,--typescript 已被弃用以支持使用 --template typescript

            我确实设法通过执行以下操作来完成所有工作:

            1. 卸载全局 create-react-app npm uninstall create-react-app -g
            2. 验证 npm 缓存npm cache verify
            3. 关闭终端。我使用 mac 终端,如果使用 IDE 可能会关闭并重新打开。
            4. 重新打开终端,浏览到您想要项目的位置并使用新的模板命令约定通过 npx 运行 create-react-app。为了让它工作,我使用了文档站点中的 typescript my-app 来确保一致性:npx create-react-app my-app --template typescript

            如果它有效,您应该会看到多个安装:一个用于 react-scripts,一个用于模板。错误消息也应该不再出现。

            【讨论】:

              【解决方案10】:

              首先通过这个命令全局卸载create-react-app:

              npm uninstall -g create-react-app
              

              然后在你的项目目录中:

              npm install create-react-app@latest
              

              终于:

              npx create-react-app my-app
              

              【讨论】:

              • 这个帮我TY
              • 经过大约 1 年的尝试,终于解决了这个问题。非常感谢
              • 在上面十几个无用的答案之后,只有一个有效,谢谢!
              【解决方案11】:

              对于 Linux,这对我有用

              sudo npm uninstall -g create-react-app
              npx create-react-app my-test-app
              

              【讨论】:

                【解决方案12】:

                1)

                npm uninstall -g create-react-app
                

                yarn global remove create-react-app
                

                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
                

                【讨论】:

                • 我不得不删除安装文件夹,npm uninstall create-react-app -g 不起作用
                • $ echo '#!/bin/sh' > /usr/local/bin/create-react-app $ echo 'echo 改用 yarn create-react-app' > /usr/local/ bin/create-react-app $ chmod 0700 /usr/local/bin/create-react-app
                • 在 Windows 上,您可以使用命令 where 而不是 which
                【解决方案13】:

                这对我有用!

                1) npm uninstall -g create-react-app

                2) npm install -g create-react-app

                3)npx create-react-app app_name

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

                【讨论】:

                  【解决方案14】:

                  这两个步骤对我有用

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

                  npm uninstall -g create-react-app
                  

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

                  npx create-react-app project-name
                  

                  【讨论】:

                    【解决方案15】:

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

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

                    然后

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

                    然后

                    1. 安装反应:npx create-react-app my-app 跟随:https://create-react-app.dev/

                    【讨论】:

                    • 是的,这是正确的 - 然后检查它是否仍然安装 $ react-create-app --version。如果还有一个版本,您可能需要手动删除它。在我的情况下,它仍然是 ~/.nvm/versions/node//lib/node_modules 所以你必须 rm -rf react-create-app 在那个文件夹中
                    【解决方案16】:

                    先清空你的 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 是个好主意。最后使用 @latest 选项创建一个新的 reactjs 应用,如下所示:npx create-react-app@latest。在使用 npx create-react-app 时没有创建模板的问题上一直存在很多困惑,如果您按照我上面(1-6)所述的步骤进行操作,那么我希望您能成功。

                    附言

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

                    npx create-react-app@latest ./client

                    祝你好运。

                    【讨论】:

                      【解决方案17】:

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

                      【讨论】:

                        【解决方案18】:

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


                        第一步:来自官方手册,

                        如果您之前通过npm install -g create-react-app 全局安装了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 安装它。

                        NB:我在最后一步成功了。

                        【讨论】:

                        • 无论出于何种原因,npxnpm init 都不起作用,但 yarn 在清除缓存后起作用。我也需要rm -rf create-react-app 文件夹。非常感谢 SO。
                        • 在 windows 上(来自 wsl 终端)which create-react-app 表明我需要删除 /mnt/c/Users/myWindowsUser/AppData/Roaming/npm/ 中的 create-react-app* 文件
                        【解决方案19】:

                        npm uninstall -g create-react-app 在某些情况下可能是一个答案,但不是我的。

                        您应该手动删除位于~/.node/bin//usr/bin/ 的create-react-app(只需输入which create-react-app 并将其从您使用rm -rf 看到的位置删除),然后运行npm i -g create-react-app

                        之后create-react-app 将正常工作。

                        【讨论】:

                          【解决方案20】:

                          先卸载create-react-app

                          npm uninstall -g create-react-app
                          

                          然后运行yarn create react-app my-appnpx create-react-app my-app

                          然后运行yarn create react-app my-appnpx create-react-app my-app 可能仍然会报错,

                          未提供模板。这可能是因为您使用的是过时版本的 create-react-app。请注意,不再支持全局安装 create-react-app。

                          这可能是因为现金。所以下一次运行

                          npm cache clean --force 
                          

                          然后运行

                          npm cache verify
                          

                          现在一切都清楚了。现在运行

                          yarn create react-app my-appnpx create-react-app my-app

                          现在你会得到你所期望的!

                          【讨论】:

                            【解决方案21】:

                            TLDR:使用 npm uninstall -g create-react-app 卸载全局包并使用 npx create-react-app app 生成新的 React 应用程序。


                            问题

                            您使用的是旧版本的 create-react-app,您已使用 npm 全局安装。 create-react-app 命令调用这个全局包。

                            您可以通过运行npm outdated -g create-react-app 或比较create-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(来自多次安装,或者因为您使用了节点版本管理器,例如 nvm)。这是一个单独的问题,我不会在这里解决,但在 answer 中有一些信息。

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


                            补充

                            全局 npm 包和npx 命令

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

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

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

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

                            使用npxcreate-react-app

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

                            yarnnpm 全局安装

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

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

                            【讨论】:

                              【解决方案22】:

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

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

                              【讨论】:

                                【解决方案23】:

                                这对我有用 1.首先通过这个命令全局卸载create-react-app:

                                npm uninstall -g create-react-app
                                

                                如果您仍然有以前的安装,请完全删除名为我的应用程序的文件夹。(确保没有程序正在使用该文件夹,包括您的终端或 cmd 提示符)

                                2.然后在你的项目目录中:

                                npm install create-react-app@latest
                                

                                3.最后:

                                npx create-react-app my-app
                                

                                【讨论】:

                                  【解决方案24】:

                                  对于 Windows 10,我必须手动删除 yarn 缓存中的一些文件夹,我的路径类似于 C:\Users\username\AppData\Local\Yarn\Cache\v1,而我必须删除的文件夹类似于 npm-create-react-app-1.0.0-1234567890abcdef

                                  【讨论】:

                                  • 对于 Windows 7,这也解决了我的问题。一个我删除了 Yarn 缓存并运行npm cache clear --force,等等我能够运行npx create react app
                                  【解决方案25】:

                                  我遇到了同样的问题,我已经在我的机器上全局安装了“create-react-app”。 有错误:

                                  “未提供模板。这可能是因为您使用的 create-react-app 版本过时。 请注意,不再支持全局安装 create-react-app。”

                                  然后我使用此命令删除了以前的安装。

                                  npm 卸载 -g create-react-app

                                  然后安装新的 react 应用。

                                  npx create-react-app new-app

                                  【讨论】:

                                    【解决方案26】:

                                    我在 Mac 上通过从全局 npm 库中卸载 create-react-app 来解决此问题,基本上就是这么说的,尝试去做,你还需要做 sudo

                                    sudo npm uninstall -g create-react-app
                                    

                                    然后简单地运行:

                                    npx create-react-app my-app-name
                                    

                                    现在应该一切都好,得到如下文件夹结构:

                                    【讨论】:

                                    • 我在 Linux Mint 19(基于 Ubuntu 的系统)上尝试了上述所有这些方法。没有任何帮助,但这确实做到了。 sudo npm uninstall -g create-react-app
                                    【解决方案27】:

                                    使用命令 npm uninstall -g create-react-app 对我不起作用。

                                    但这有效:

                                    yarn global remove create-react-app

                                    然后:

                                    npx create-react-app my-app

                                    【讨论】:

                                    • 这对我有用,我尝试了所有方法,包括从 /usr/local/bin 手动删除
                                    【解决方案28】:

                                    这项工作是给我的:

                                    让我们通过这个命令全局卸载 create-react-app:

                                    npm uninstall -g create-react-app
                                    

                                    之后在你的项目目录中:

                                    npm install create-react-app@latest
                                    

                                    最后:

                                    npx create-react-app my-app
                                    

                                    对于打字稿:

                                    npx create-react-app my-app --template typescript
                                    

                                    【讨论】:

                                      【解决方案29】:

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

                                      TLDR;运行npx --ignore-existing create-react-app

                                      我在 Mac 上使用Mojave 10.15.2

                                      没有全局安装 CRA - 在 /usr/local/lib/node_modules/usr/local/bin 中也没有找到它。

                                      然后我在 CRA 的 github 问题上遇到了这个 comment。使用--ignore-existing 标志运行命令会有所帮助。

                                      【讨论】:

                                      • "npx: --ignore-existing 参数已被删除。"但到目前为止,没有一个答案能解决这个问题。
                                      • 我卖掉了我的 Mac 并在新的 Mac 上进行了全新安装...听起来很奇怪...
                                      【解决方案30】:

                                      尝试以管理员身份运行您的终端。我遇到了同样的问题,除了以管理员身份打开终端然后执行 npx create-react-app yourAppName 之外没有任何帮助

                                      【讨论】:

                                        猜你喜欢
                                        • 1970-01-01
                                        • 2022-12-06
                                        • 2018-07-07
                                        • 2020-04-11
                                        • 2019-09-13
                                        • 1970-01-01
                                        • 2017-05-02
                                        相关资源
                                        最近更新 更多