我正面临着下面的错误。我尝试了所有的解决方案中提到的其他职位有关同一问题。但没有运气。请帮助。
错误:-错误在./src/index.js 6:16模块解析失败:意外标记(6:16)您可能需要适当的加载程序来处理此文件类型,当前没有配置加载程序来处理此文件。请参阅https://webpack.js.org/concepts#loa rs|从“./components/App”导入应用程序;|
reactDOM.render(,文档.getElementById(“应用程序”)); i?wdm?:无法编译。
项目结构Project structure image
以下是我的文件。
package.json
{
"name": "reactapps",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --hot"
},
"author": "nivs",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"bable-loader": "0.0.1-security",
"html-webpack-plugin": "^4.2.0",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
},
"dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1"
}
}
webpack.conf.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'build'),
publicPath: '/',
filename: 'bundle.js'
},
devServer: {
contentBase: './build',
publicPath: '/dist/'
},
module: {
rules: [{
test: /\.js$|jsx/,
exclude: /node_modules/,
use: ['babel-loader']
}]
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve('./index.html'),
}),
]
};
巴伯尔
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
App.js
import React,{Component} from 'react';
class App extends Component{
render() {
return (
<div><h1>Hello</h1></div>
);
}
};
export default App;
index.js
import React from 'react'
import ReactDOM from 'react-dom';
import App from './components/App';
ReactDOM.render(<App />,document.getElementById("app"));
1条答案
按热度按时间holgip5t1#
查看您的package.json,似乎是babel版本导致了该错误。如果您使用
webpack
4.x,则应使用babel-loader
8.x和babel
7.x,see webpack - babel-loader尝试升级babel-*,您可以从Here获取待办事项列表