当我在我的终端上输入create-react-app my-app命令时,它似乎工作了-下载所有库成功等。在该过程结束时,我得到一个消息,没有提供模板。

输入

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的Json:

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

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

知道这是怎么回事吗?


当前回答

最简单的方法之一就是使用

npx --ignore-existing create-react-app [project name]

这将删除create-react-app的旧缓存版本,然后获取新版本来创建项目。

注意:添加项目名称很重要,因为忽略现有的create-react-app版本是过时的,并且机器全局环境中的更改是临时的,因此以后仅使用npx create-react-app[项目名称]将无法提供所需的结果。

其他回答

首先卸载create-react-app

npm uninstall -g create-react-app

然后运行yarn create react-app my-app或npx create-react-app my-app

然后运行yarn create react-app my-app或NPX create-react-app my-app可能仍然会给出错误,

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

这可能是因为现金的原因。所以下一组

npm cache clean --force 

然后运行

npm cache verify

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

Yarn create react-app my-app或NPX create-react-app my-app

现在你会得到你想要的!

最简单的方法之一就是使用

npx --ignore-existing create-react-app [project name]

这将删除create-react-app的旧缓存版本,然后获取新版本来创建项目。

注意:添加项目名称很重要,因为忽略现有的create-react-app版本是过时的,并且机器全局环境中的更改是临时的,因此以后仅使用npx create-react-app[项目名称]将无法提供所需的结果。

虽然这里已经有很多答案了。 当我面对这种情况时,我想出了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

你可以使用下面这些命令:

create-react-app my-app NPM init react-app my-app Yarn创建my-app


第二步(如果第一步不管用):

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

NPM卸载-g create-react-app NPM缓存清理——force NPM缓存验证 Yarn创建my-app


第三步:(如果这两步都不行) 首先通过NPM uninstall -g create-react-app卸载,然后检查你的命令行上的create-react-app命令是否“安装”了它。如果你得到(/usr/local/bin/create-react-app)这样的文件,那么运行rm -rf /usr/local/bin/create-react-app(文件夹可能不同)手动删除。 然后再次通过npx/npm/yarn安装它。

NB:最后一步我成功了。

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

从“/usr/local/bin/”目录下删除节点

then

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

then

安装react: npx create-react-app my-app follow: https://create-react-app.dev/

如果你之前已经通过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。

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

NPM卸载-g create-react-app NPM install -g create-react-app create-react-app my-app