我正在尝试安装react,但是在执行npm start时遇到错误。 我已经尝试了许多有关更改Babel版本的解决方案,但仍然无法完全解决它。
错误:
C:\Users\support\Desktop\reactApp>npm start
> reactapp@1.0.0 start C:\Users\support\Desktop\reactApp
> webpack-dev-server --mode development --open --hot
i 「wds」: Project is running at http://localhost:8001/
i 「wds」: webpack output is served from /
i 「wds」: Content not from webpack is served from C:\Users\support\Desktop\reactApp
i 「wdm」: wait until bundle finished: /
× 「wdm」: Hash: 8012f13b5074e55019d9
Version: webpack 4.43.0
Time: 645ms
Built at: 06/02/2020 2:29:04 PM
Asset Size Chunks Chunk Names
index.html 272 bytes [emitted]
index_bundle.js 395 KiB main [emitted] main
Entrypoint main = index_bundle.js
[0] multi (webpack)-dev-server/client?http://localhost:8001 (webpack)/hot/dev-server.js ./main.js 52 bytes {main} [built]
[./main.js] 1.37 KiB {main} [built] [failed] [1 error]
[./node_modules/webpack-dev-server/client/index.js?http://localhost:8001] (webpack)-dev-server/client?http://localhost:8001 4.29 KiB {main} [built]
[./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.51 KiB {main} [built]
[./node_modules/webpack-dev-server/client/socket.js] (webpack)-dev-server/client/socket.js 1.53 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/createSocketUrl.js] (webpack)-dev-server/client/utils/createSocketUrl.js 2.91 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/log.js] (webpack)-dev-server/client/utils/log.js 964 bytes {main} [built]
[./node_modules/webpack-dev-server/client/utils/reloadApp.js] (webpack)-dev-server/client/utils/reloadApp.js 1.59 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/sendMessage.js] (webpack)-dev-server/client/utils/sendMessage.js 402 bytes {main} [built]
[./node_modules/webpack-dev-server/node_modules/strip-ansi/index.js] (webpack)-dev-server/node_modules/strip-ansi/index.js 161 bytes {main} [built]
[./node_modules/webpack/hot sync ^\.\/log$] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes {main} [built]
[./node_modules/webpack/hot/dev-server.js] (webpack)/hot/dev-server.js 1.59 KiB {main} [built]
[./node_modules/webpack/hot/emitter.js] (webpack)/hot/emitter.js 75 bytes {main} [built]
[./node_modules/webpack/hot/log-apply-result.js] (webpack)/hot/log-apply-result.js 1.27 KiB {main} [built]
[./node_modules/webpack/hot/log.js] (webpack)/hot/log.js 1.34 KiB {main} [built]
+ 20 hidden modules
ERROR in ./main.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module 'babel-preset-env' from 'C:\Users\support\Desktop\reactApp'
- Did you mean "@babel/env"?
at Function.resolveSync [as sync] (C:\Users\support\Desktop\reactApp\node_modules\resolve\lib\sync.js:89:15)
at resolveStandardizedName (C:\Users\support\Desktop\reactApp\node_modules\@babel\core\lib\config\files\plugins.js:101:31)
at resolvePreset (C:\Users\support\Desktop\reactApp\node_modules\@babel\core\lib\config\files\plugins.js:58:10)
at loadPreset (C:\Users\support\Desktop\reactApp\node_modules\@babel\core\lib\config\files\plugins.js:77:20)
at createDescriptor (C:\Users\support\Desktop\reactApp\node_modules\@babel\core\lib\config\config-descriptors.js:154:9)
at C:\Users\support\Desktop\reactApp\node_modules\@babel\core\lib\config\config-descriptors.js:109:50
at Array.map (<anonymous>)
at createDescriptors (C:\Users\support\Desktop\reactApp\node_modules\@babel\core\lib\config\config-descriptors.js:109:29)
at createPresetDescriptors (C:\Users\support\Desktop\reactApp\node_modules\@babel\core\lib\config\config-descriptors.js:101:10)
at C:\Users\support\Desktop\reactApp\node_modules\@babel\core\lib\config\config-descriptors.js:58:104
Child HtmlWebpackCompiler:
1 asset
Entrypoint HtmlWebpackPlugin_0 = __child-HtmlWebpackPlugin_0
[./node_modules/html-webpack-plugin/lib/loader.js!./index.html] 501 bytes {HtmlWebpackPlugin_0} [built]
i 「wdm」: Failed to compile.
这些是我的以下文件
webpack.comfig.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './main.js',
output: {
path: path.join(__dirname, '/bundle'),
filename: 'index_bundle.js'
},
devServer: {
inline: true,
port: 8001
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['env', 'react']
}
}
]
},
plugins:[
new HtmlWebpackPlugin({
template: './index.html'
})
]
}
package.json
{
"name": "reactapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --mode development --open --hot",
"build": "webpack --mode production"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.11.0"
},
"devDependencies": {
"@babel/core": "^7.10.2",
"@babel/preset-env": "^7.10.2",
"@babel/preset-react": "^7.10.1",
"babel-loader": "^8.1.0",
"html-webpack-plugin": "^4.3.0"
}
}
.babelrc
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
]
}
答案 0 :(得分:1)
您正在使用babel预设7 +
在您的webpack.config.js中执行此操作
presets: ['@babel/preset-env']
答案 1 :(得分:0)
在webpack.config中-不需要在下面,因为您拥有的babelrc文件涵盖了该内容。尝试将其从webpack中删除。
query: {
presets: ['env', 'react']
}