即使webpack正确生成bundle.js,WEBPACK DEV SERVER仍未运行

时间:2016-09-03 08:27:24

标签: gulp ecmascript-6 webpack webpack-dev-server

我尝试在Gulp任务中运行webpack-dev-server,但即使webpack正确生成bundle.jswebpack-dev-server也未运行。

当我拨打计算机的http://[IP地址时:8080',它说“这个网站无法到达”。

以下是Gulp任务:

import webpack from 'webpack';
import config from './webpack.config';
import WebpackDevServer from 'webpack-dev-server';

...

gulp.task('serve', () => {
  const compiler = webpack(config);
  const devServer = new WebpackDevServer(compiler, {
    quiet: false
  }).listen(8080,  'localhost', (err) => {
    if(err) {
      console.log(err);
    }
  });
});

webpack.config.js是:

const BUILD_DIR = path.resolve(__dirname, 'dist/');
const APP_DIR = path.resolve(__dirname, 'client');

export default {
    entry: APP_DIR + '/index.jsx',
    output: {
        path: BUILD_DIR,
        filename: 'bundle.js'
    }
};

结果我得到以下输出:

[10:29:52] Requiring external module babel-register
[10:29:55] Using gulpfile /Volumes/MACDATA/Current_Work/ES6Twitter/project/Hooter/gulpfile.babel.js
[10:29:55] Starting 'serve'...
[10:29:55] Finished 'serve' after 50 ms
Hash: 503283de8850d977b4ec
Version: webpack 1.13.2
Time: 53ms
    Asset     Size  Chunks             Chunk Names
bundle.js  1.42 kB       0  [emitted]  main
chunk    {0} bundle.js (main) 29 bytes [rendered]
    [0] ./client/index.jsx 29 bytes {0} [built]
webpack: bundle is now VALID.

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

您指定localhost,但您想从外部计算机连接。在这种情况下,您需要将localhost更改为0.0.0.0。这使得dev服务器可以在外部机器上访问。