What to do if there is an error when starting the react project
Solution to the error when starting the react project: 1. Enter the project folder, start the project and view the error message; 2. Execute the "npm install" or "npm install react-scripts" command; 3. Execute "npm install @ant-design/pro-field --save" command.
1. Preliminary knowledge:
npm (you can also use yarn, this article starts with npm as an example)
npm introduction
- stands for Node Package Manager, which is a package management tool installed along with NodeJS.
- Allow users to download third-party packages written by others from the NPM server for local use.
- Allows users to download and install command line programs written by others from the NPM server for local use.
- Allow users to upload packages or command line programs they write to the NPM server for others to use
npm command
-
npm -v
To test whether the installation is successful - View the installed plug-ins in the current directory:
npm list
- Use npm to download the plug-in:
npm install [ -g ] [ --save-dev] <name></name>
- Use npm to update the plug-in:
npm update [ -g ] [ --save-dev ] <name> </name>
Note:
install can be abbreviated as i, [] means optional, means required
<name> </name>
: Package (plug-in library) name
[ -g ]:
Global installation. It will be installed in C:\Users\Administrator\AppData\Roaming\npm,and written to the system environment variable; global installation can be called from anywhere through the command line;
non-global Installation: It will be installed in the current location directory;, the local installation will be installed in the node_modules folder of the location directory, and is called by request;
[ --save-dev]:
Write The dependencies
of package.json
need to be published to the production environment, such as react, vue family bucket, ele-ui and other UI frameworks. The plug-ins that must be used when these projects are running need to be placed in dependencies.
##cnpm
- Domestic mirror made by Taobao team, Because npm's server is located abroad, the installation may be affected. Taobao mirror installation speed is generally faster.
- Installation: Command prompt execution
-
npm install cnpm -g --registry=https://registry.npm.taobao.org - cnpm -v
to test whether the installation is successful
Notes:
①After the project is successfully started normally, the following page will appear in the browserIf you need Expose webpacke configuration (create Do not do anything after completing the project), directly execute the following code: (This operation is irreversible!)
npm run eject
//引入react核心组件主库 import React, { Component } from 'react' //引入ReactDOM 子库 import ReactDOM from 'react-dom'
三、启动项目时可能出现的报错:
1. 'react-app-rewired' 不是内部或外部命令,也不是可运行的程序或批处理文件。
原因:可能是由于create-react-app出现丢包缺陷,手动安装包后,需要重新安装,这样node_modules/.bin/目录下才会重新出现react-scripts的文件,从而解决问题。
解决:npm install 或 npm install react-scripts
(若因为某些原因导致包出故障,就删除node_modules文件夹,重新npm install )
2.
./src/App.jsx
Module not found: Can't resolve '@ant-design/icons' in 'C:\Users\...
原因:没有安装@ant-design/pro-field
解决:npm install @ant-design/pro-field --save
四、Todolist项目相关库:
npm i prop-types //对接收的props进行:类型、必要性的限制 import PropTypes from 'prop-types' npm i nanoid //生成唯一标识 一般用来充当id或遍历时的index import {nanoid} from 'nanoid' id:nanoid()
五、GitHub搜索案例相关库:
npm install pubsub-js --save //消息订阅-发布机制 import PubSub from 'pubsub-js' npm install axios //轻量级ajax请求库 import axios from 'axios'
六、尚硅谷路由案例相关库:
npm install --save react-router-dom //路由库,前端路由:value是component,用于展示页面内容; // 后端路由:value是function, 用来处理客户端提交的请求。 import {BrowserRouter,HashRouter,NavLink,Link,Route} from 'react-router-dom' // V5及之前的版本才有以下三个 import {Switch,Redirect,withRouter} from 'react-router-dom' // Switch:懒惰匹配 Redirect:重定向 withRouter:让一般组件具备路由组件所特有的API npm i -save-dev query-string // 对http请求所带的数据进行解析 import qs from 'querystring' import qs from 'qs' // qs.parse() 将字符串解析为对象 // qs.stringify() //将对象解析为字符串(urlencoded编码)
七、UI库案例相关库:
//开源React UI组件库 npm i antd // 主库 import { Button,DatePicker } from 'antd'; // 子库 图标等 import {WechatOutlined,WeiboOutlined,SearchOutlined} from '@ant-design/icons' // const 要写在 import后面 const { RangePicker } = DatePicker; //按需引入 自定义主题步骤: //1.安装依赖 yarn add react-app-rewired customize-cra babel-plugin-import less less-loader //2.修改package.json "scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test", "eject": "react-scripts eject" }, //3.根目录下创建config-overrides.js const { override, fixBabelImports,addLessLoader} = require('customize-cra'); module.exports = override( fixBabelImports('import', { libraryName: 'antd', libraryDirectory: 'es', style: true, }), addLessLoader({ lessOptions:{ javascriptEnabled: true, modifyVars: { '@primary-color': 'green' }, } }), );
八、redux相关库:
// 一、基本redux componnet==>一般组件Count redux文件==>action、reducer、store.js npm i redux // redux异步action npm i redux-thunk // redux中,最为核心的store对象将state、action、reducer联系在一起的对象 // 1.建立store.js文,引入createStore,专门用于创建store对象 // 引入redux-thunk,applyMiddleware,用于支持异步action import {createStore,applyMiddleware} from 'redux' import thunk from 'redux-thunk' // 2.引入为Count组件服务的reducer import countReducer from './count_reducer' // 3. 语法:const store = createStore(reducer) // store.js文件中一般如下: export default createStore(countReducer,applyMiddleware(thunk)) // 4.store对象的功能 1)store.getState(): 得到state 2)store.dispatch({type:'INCREMENT', number}): 分发action, 触发reducer调用, 产生新的state 3)store.subscribe(render): 注册监听, 当产生了新的state时, 自动调用
// 二、react-redux 容器组件[UI(同名)组件] : UI组件==>一般组件 containers组件==>外壳 npm i react-redux //容器组件中,引入connect用于连接UI组件与redux // Provider让多个组件都可以得到store中state数据 import {connect,Provider} from 'react-redux' //定义UI组件 class CountUI extends Component{...} // 使用connect()()创建并暴露一个Count的容器组件 export default connect(mapStateToProps,mapDispatchToProps)(CountUI) <Count store={store} /> // 给容器组件传递store 连接外部的redux; connect()()用于连接内部的内部的UI组件 // 数据共享 // store.js汇总所有的reducer变为一个总的reducer import {combineReducers} from 'redux' const allReducer = combineReducers({ he:countReducer, rens:personReducer }) // containers组件中: connect( state => ({key:value}), //映射状态 mapStateToProps {key:xxxAction} //映射操作状态的方法 mapDispatchToProps )(UI组件) // redux开发者工具 chrome网上商店中搜索安装 Redux Devtools 工具 npm i redux-devtools-extension import {composeWithDevTools} from 'redux-devtools-extension' export default createStore(reducer,composeWithDevTools(applyMiddleware(thunk)))
推荐学习:《react视频教程》
The above is the detailed content of What to do if there is an error when starting the react project. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Share the simple and easy-to-understand PyCharm project packaging method. With the popularity of Python, more and more developers use PyCharm as the main tool for Python development. PyCharm is a powerful integrated development environment that provides many convenient functions to help us improve development efficiency. One of the important functions is project packaging. This article will introduce how to package projects in PyCharm in a simple and easy-to-understand way, and provide specific code examples. Why package projects? Developed in Python

Fermat's last theorem, about to be conquered by AI? And the most meaningful part of the whole thing is that Fermat’s Last Theorem, which AI is about to solve, is precisely to prove that AI is useless. Once upon a time, mathematics belonged to the realm of pure human intelligence; now, this territory is being deciphered and trampled by advanced algorithms. Image Fermat's Last Theorem is a "notorious" puzzle that has puzzled mathematicians for centuries. It was proven in 1993, and now mathematicians have a big plan: to recreate the proof using computers. They hope that any logical errors in this version of the proof can be checked by a computer. Project address: https://github.com/riccardobrasca/flt

Title: Learn more about PyCharm: An efficient way to delete projects. In recent years, Python, as a powerful and flexible programming language, has been favored by more and more developers. In the development of Python projects, it is crucial to choose an efficient integrated development environment. As a powerful integrated development environment, PyCharm provides Python developers with many convenient functions and tools, including deleting project directories quickly and efficiently. The following will focus on how to use delete in PyCharm

PyCharm is a powerful Python integrated development environment that provides a wealth of development tools and environment configurations, allowing developers to write and debug code more efficiently. In the process of using PyCharm for Python project development, sometimes we need to package the project into an executable EXE file to run on a computer that does not have a Python environment installed. This article will introduce how to use PyCharm to convert a project into an executable EXE file, and give specific code examples. head

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

IDEA (IntelliJIDEA) is a powerful integrated development environment that can help developers develop various Java applications quickly and efficiently. In Java project development, using Maven as a project management tool can help us better manage dependent libraries, build projects, etc. This article will detail the basic steps on how to create a Maven project in IDEA, while providing specific code examples. Step 1: Open IDEA and create a new project Open IntelliJIDEA

PyCharm is a powerful Python integrated development environment (IDE) that provides rich functions to help developers write and manage Python projects more efficiently. In the process of developing projects using PyCharm, sometimes we need to delete some projects that are no longer needed to free up space or clean up the project list. This article will detail how to delete projects in PyCharm and provide specific code examples. How to delete a project Open PyCharm and enter the project list interface. In the project list,
