模块构建失败 - Webpack、React、Babel

Module Build Failed - Webpack, React, Babel

我正在关注 plural sight 的视频教程。课程名称是 "Building a Real-time App with React, Flux, Webpack, and 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"
 }
 }

已解决。答案是安装预设 npm i --save babel-preset-env babel-preset-react。然后在加载程序中的webpack.config.js中添加另一个键:query: {presets: ['env', 'react'] }。应该可以走了。

我尝试了上述步骤并为此关注了许多博客和网站,但问题仍然存在there.Then我发现我正在使用 webpack 4。所以, 经过长时间的搜索,我发现了博客:
https://medium.freecodecamp.org/part-1-react-app-from-scratch-using-webpack-4-562b1d231e75.
因此,我按照步骤进行操作,发现问题仍然是 there.Then,经过长时间搜索,我发现 node_modules folder.Then 中不存在 react 文件夹 我按照以下步骤操作:

  1. 删除 package.lock.json 文件。
  2. 运行 npm 安装。
  3. 检查 node_modules 文件夹,您现在将看到 react 文件夹。
  4. 运行 npm 开始。 然后,我的问题得到了解决。

你可以尝试执行这个命令:npm rebuild node-sass