最近,使用 npx creact-react-app
创建项目可能会提示:
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.
创建项目后只生成了node_modules
, package.json
, package_lock.json
或 yarn.lock
。
原因是最近 create-react-app
更新了新版本,如果以前将create-react-app
安装在本地,即执行了 npm install -g create-react-app
就会报此错误。
解决方案
卸载 create-react-app
,执行:
npm uninstall -g create-react-app
以确保 npx
使用最新版本创建项目。
create-react-app创建应用方法
本地开发环境需要保证NodeJS的版本>=8.10
- 通过
npx
创建:
npx create-react-app my-app
- 通过
npm
创建:
npm init react-app my-app
- 通过
yarn
(0.25+) 创建:
yarn create react-app my-app
选择模板
通过在命令后添加 --template [template-name]
来选择模板。
模板名的格式为 cra-template-template-name
,因此你可以在 npm
中找到很多有用的社区模板。
但是在创建应用指定模板时,你可以省略掉前缀 cra-template-
。
![7c57d4b574d76a2376b7165a911bd5cc.png](https://i-blog.csdnimg.cn/blog_migrate/429a1c6223136b12112b6ce6a78e3ae4.jpeg)
例如创建一个使用 TypeScript
的 React
应用:
npx create-react-app my-app --template typescript
使用 npm
作为包管理器
默认将使用 yarn
作为包管理器,因为它使用起来比较简单方便,例如添加一个react-spring
依赖:
yarn add react-spring
而不是:
npm install react-spring
如果只想使用 npm
作为包管理器的话,只需要追加 --use-npm
。
npx create-react-app my-app --use-npm