1. 程式人生 > 其它 >npm react版本_npx creact-react-app未完整生成專案的解決方案

npm react版本_npx creact-react-app未完整生成專案的解決方案

技術標籤:npm react版本

最近,使用 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.jsonyarn.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-

cra-template-* - npm search​www.npmjs.com 7c57d4b574d76a2376b7165a911bd5cc.png

例如建立一個使用 TypeScriptReact 應用:

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