2017-10-13 100 views
-1

我想我錯過了webpack.config.js中的東西。當我嘗試生成bundle.js時,出現此錯誤。webpack3:你可能需要一個合適的加載器來處理這個文件

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') 
); 

回答

0
在webpack.config.js

嘗試切換

use: ["babel-loader"], 

loader: "babel-loader", 

隨着Webpack 2 migration tutorial說,useloader之間的區別是,當我們希望裝載機的數組我們必須使用use,如果它只是一個加載程序,我們必須使用loader

+0

不,我仍然得到相同的錯誤。 – Rahul

+0

如果您有時間,請更正此項目的github回購中的此錯誤 – Rahul

+0

https://github.com/RMCoder198/React-with-node – Rahul

相關問題