我正在关注多视点的视频教程。课程名称为"使用React,Flux,Webpack和Firebase构建实时应用程序"。
请参阅以下代码和我所遇问题的附加屏幕截图。当我尝试重新构建文件时,Webpack失败了。有人可以告诉你这个问题是什么。我目前正在使用所有最新的图书馆。
/*webpack.config.js*/
module.exports = {
entry: {
main: [
'./src/main.js'
]
},
output: {
filename: './public/[name].js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel'
}
]
}
}
/*App.jsx*/
import React from 'react';
class App extends React.Component {
constructor() {
super();
this.state = {
messages: [
'hi there how are you ?',
'i am fine, how are you ?'
]
}
}
render() {
var messageNodes = this.state.messages.map((message)=> {
return (
<div>{message}</div>
);
});
return (
<div>{messageNodes}</div>
);
}
}
export default App;
/*main.js*/
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App.jsx';
ReactDOM.render(<App/>, getElementById('container'));
/*index.html*/
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<div id="container"></div>
<script src="public/main.js"></script>
</body>
</html>
/*package.json */
{
"name": "reatapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "^6.1.2",
"babel-loader": "^6.0.1",
"babel-preset-react": "^6.1.2",
"babelify": "^7.2.0",
"react": "^0.14.2",
"react-dom": "^0.14.2",
"webpack": "^1.12.3"
}
}
答案 0 :(得分:12)
它解决了。答案是安装预设npm i --save babel-preset-es2015 babel-preset-react
。然后在加载器中添加 webpack.config.js 中的另一个键:query: {presets: ['es2015', 'react'] }
。应该好好去。
答案 1 :(得分:0)
我尝试了上面的步骤,并且关注了许多博客和网站,但问题仍然存在。然后我发现我正在使用webpack 4.所以,
经过长时间的搜索,我发现了博客:
https://medium.freecodecamp.org/part-1-react-app-from-scratch-using-webpack-4-562b1d231e75。
所以,我按照步骤发现问题仍然存在。然后,经过长时间的搜索,我发现我的node_modules文件夹中没有react文件夹。然后我按照以下步骤操作:
答案 2 :(得分:0)
您可以尝试执行以下命令:npm rebuild node-sass