2016-11-27 100 views
0

我剛剛剛接觸webpack並作出反應,只是通過文檔並創建了一個示例來工作。不幸的是我卡住了,無法繼續。問題是沒有生成捆綁文件。 我創建的文件是未生成Webpack捆綁文件

的package.json

{ 
    "name": "rohith", 
    "version": "1.0.0", 
    "description": "", 
    "main": "index.js", 
    "scripts": { 
    "start": "webpack-dev-server" 
    }, 
    "author": "", 
    "license": "ISC", 
    "dependencies": { 
    "react": "^15.4.1", 
    "react-dom": "^15.4.1", 
    "webpack": "^1.13.3", 
    "webpack-dev-server": "^1.16.2" 
    } 
} 

webpack.config.js

module.export = { 
    entry : './main.js', 
    output : { 
     path : './', 
     filename : 'index.js' 
    }, 
    devServer : { 
     inline : true, 
     port : 3333 
    }, 
    module : { 
     loaders : [ 
      { 
       test : /\.js$/, 
       exclude : /node_modules/, 
       loader : 'babel', 
       query : { 
        presets : ['es2015','react'] 
       } 
      } 
     ] 
    } 
} 

App.js

import React from 'react'; 
class App extends React.Component { 
    render(){ 
     return <div>Hello</div> 
    } 
} 

export default App 

main.js

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import App from './App'; 
ReactDOM.render(<App />,document.getElementById('app')); 

的index.html

<!DOCTYPE html> 
<html lang = "en"> 
<head> 
    <meta charset = "UTF-8"> 
    <title>Setup</title> 
</head> 
<body> 
    <div id = "app"></div> 
    <script src ="index.js"></script> 
</body> 
</html> 

我得到那個包是有效的,但不產生index.js。 不能在本地主機3333

由於運行,

回答

1

在webpack.config.js,使用module.exports代替module.export。請參閱Output filename not configured Error in Webpack 另請注意,您的package.json缺少一些依賴關係。這是更新的package.json,它的工作原理:

{ 
"name": "rohith", 
"version": "1.0.0", 
"description": "", 
"main": "index.js", 
"scripts": { 
    "start": "webpack-dev-server" 
}, 
"author": "", 
"license": "ISC", 
"dependencies": { 
    "react": "^15.4.1", 
    "react-dom": "^15.4.1", 
    "webpack": "^1.13.3", 
    "webpack-dev-server": "^1.16.2" 
}, 
"devDependencies": { 
    "babel": "^6.5.2", 
    "babel-core": "^6.18.2", 
    "babel-loader": "^6.2.8", 
    "babel-preset-es2015": "^6.18.0", 
    "babel-preset-react": "^6.16.0" 
} 
} 
6

我認爲問題是,你沒有給絕對的輸出路徑。

試試這個:

var path = require('path'); 

module.exports = { 
    entry : './main.js', 
    output : { 
     path : path.join(__dirname, './'), 
     filename : 'index.js' 
    }, 
    devServer : { 
     inline : true, 
     port : 3333 
    }, 
    module : { 
     loaders : [ 
      { 
       test : /\.js$/, 
       exclude : /node_modules/, 
       loader : 'babel', 
       query : { 
        presets : ['es2015','react'] 
       } 
      } 
     ] 
    } 
} 

希望它能幫助:)

2

替換你的腳本與以下對象:

"scripts": { 
    "start": "npm run build", 
    "build": "webpack -p && webpack-dev-server" 
    }, 

然後,運行$ npm start

0

對我的問題與「腳本」下的package.json一起

這裏是修復:

裏面你的package.json文件,腳本下增加以下內容:

「腳本」:{
「開始」: 「的WebPack-dev的服務器」,
「建」: 「的WebPack -p」 }

首先,我跑了構建然後開始。

紗建立

,如果你先建那麼你bundle.js文件將被創建之後,你可以使用這個命令:

紗線開始