webpack3:您可能需要一个合适的加载程序来处理此文件

时间:2017-10-13 16:29:34

标签: javascript reactjs webpack loader

我想我错过了webpack.config.js中的某些内容。当我尝试生成bundle.js时,我收到此错误。

 ERROR in ./entry.js
Module parse failed: /home/rahul/project/src/entry.js Unexpected token (9:2)

您可能需要适当的加载程序来处理此文件

| 
| ReactDOM.render(
|   <Router history={browserHistory}>
|       <Route path="/register" component={Reg}>
|         <IndexRoute component={Home}/>

package.json

{
"name": "project",
"version": "1.0.0",
"description": "React with node and express",
"main": "entry.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "Rahul singh mawari",
"license": "MIT",
"dependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"express": "^4.16.2",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-router": "^4.2.0"
},
"devDependencies": {
"babel-loader": "^7.1.2",
"http-server": "^0.10.0",
"webpack": "^3.7.1"
}
}

webpack.config.js

var webpack = require('webpack');
var path = require('path');


var config = { 
entry: path.join(__dirname,'src', 'entry.js'),
output: {
    path: path.join(__dirname,'src', 'static'),
filename: 'bundle.js',
},

module: {
rules: [
        {
            test: /\.js?$/, 
            exclude:/node_modules/,
            use: ["babel-loader"],
            query: {
presets: ['es2015', 'react']
}
}
]
}
};
module.exports = config;

入口点的entry.js

import react from 'react';
import Reg from './components/register';
import Home from './components/home'; 
import ReactDOM from 'react-dom'; 
import { Router, Route, Link, browserHistory, IndexRoute } from 
'react-router';


ReactDOM.render(
<Router history={browserHistory}>
  <Route path="/register" component={Reg}>
    <IndexRoute component={Home}/>
   </Route>
  </Router>,
document.getElementById('app')
);

1 个答案:

答案 0 :(得分:0)

你的webpack.config.js中的

尝试切换

use: ["babel-loader"],

loader: "babel-loader",

正如Webpack 2 migration tutorial所说,useloader之间的区别在于我们需要一组加载器,我们必须使用use,如果它是只需一个加载器,我们必须使用loader