Webpack开发服务器未显示内容。

4
当我运行webpack dev server时,遇到了以下问题: 当我运行npm start时,它显示如下: ➜ directory git:(staging) ✗ npm start directory @1.0.0 start directory BUILD_DEV=1 BUILD_STAGING=1 ./node_modules/webpack-dev-server/bin/webpack-dev-server.js http://localhost:8080/ webpack结果从/undefined/提供。 内容从directory提供。 directory404将回退到/index.html 哈希:75773622412153d5f921 版本:webpack 1.12.11 时间:43330毫秒
我猜问题可能是由于以下行引起的: webpack结果从/undefined/提供。
当我在http://localhost:8080/打开浏览器时,它显示为:Cannot GET /

现在控制台中没有任何内容。

你有什么关于这个问题的想法吗?

更新:Webpack配置文件

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const merge = require('webpack-merge');
const nodeModulesDir = path.resolve(__dirname, 'node_modules');

const deps = [
  'moment/min/moment.min.js',
  'underscore/underscore-min.js',
];

/* Include SASS path here if you want to this in your sass files:
 *   @import 'bourbon';
 */
const bourbon = require('node-bourbon').includePaths;

const TARGET = process.env.npm_lifecycle_event;
const ROOT_PATH = path.resolve(__dirname);
const SASS_DEPS = [bourbon].toString();

const BUILD_NUMBER = process.env.CI_BUILD_NUMBER;

const common = {
  entry: path.resolve(ROOT_PATH, 'app/index.js'),
  output: {
    filename: BUILD_NUMBER + '-[hash].js',
    path: path.resolve(ROOT_PATH, 'build'),
    publicPath: `/${BUILD_NUMBER}/`,
  },
  module: {
    loaders: [
      {
        test: /\.scss$/,
        loaders: ['style', 'css', 'sass?includePaths[]=' + SASS_DEPS],
        include: path.resolve(ROOT_PATH, 'app'),
      },
      {
        test: /\.css$/,
        loaders: [
          'style',
          'css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]',
          'sass?includePaths[]=' + SASS_DEPS,
          'postcss'
        ],
        include: path.resolve(ROOT_PATH),
        exclude: /(pure\/grids|Grids).*\.css$/,
      },
      {
        test: /(pure\/grids|Grids).*\.css$/,
        loaders: [
          'style',
          'css',
          'sass?includePaths[]=' + SASS_DEPS,
        ],
        include: path.resolve(ROOT_PATH),
      },
      {
        test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: 'url-loader?limit=10000&minetype=application/font-woff',
      },
      { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: 'file-loader',
      },
      {
        test: /\.json$/,
        loader: 'json',
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'My App',
      template: path.resolve(ROOT_PATH, 'app/index.html'),
      inject: 'body',
      minify: {
        removeComments: true,
        collapseWhitespace: true,
      },
    }),
    new webpack.DefinePlugin({
      __DEV__: JSON.stringify(JSON.parse(process.env.BUILD_DEV || 'false')),
      __STAGING__: JSON.stringify(JSON.parse(process.env.BUILD_STAGING || 'false')),
      __API_HOST__: JSON.stringify(process.env.BUILD_STAGING ? 'my.api' : 'my.api'),
    }),
  ],
  resolve: {
    alias: {
      'styles': path.resolve(ROOT_PATH, 'app/styles'),
    },
    extensions: ['', '.js', '.jsx', '.json'],
  },
  postcss: function() {
    return [
      require('postcss-import'),
      require('autoprefixer'),
      require('postcss-cssnext'),
    ]
  }
};

if (TARGET === 'start' || !TARGET) {
  module.exports = merge(common, {
    output: {
      filename: '[hash].js',
      path: path.resolve(ROOT_PATH, 'build'),
      publicPath: '/',
    },
    devtool: 'eval-source-map',
    module: {
      loaders: [
        {
          test: /\.jsx?$/,
          loaders: [
            'react-hot',
            'babel-loader'
          ],
          include: path.resolve(ROOT_PATH, 'app'),
        },
      ],
    },
    devServer: {
      colors: true,
      historyApiFallback: true,
      hot: true,
      inline: true,
      progress: true,
    },
    plugins: [
      new webpack.HotModuleReplacementPlugin(),
    ],
  });
} else if (TARGET === 'build' || TARGET === 'builds') {
  const config = {
    resolve: {
      alias: {},
    },
    module: {
      loaders: [
        {
          test: /\.jsx?$/,
          loader: 'babel-loader',
          include: path.resolve(ROOT_PATH, 'app'),
        },
      ],
      noParse: [],
    },
    plugins: [
      new webpack.optimize.UglifyJsPlugin({
        minimize: true,
        compressor: {
          screw_ie8: true,
          warnings: false,
        },
        compress: {
          warnings: false,
        },
        output: {
          comments: false,
        },
      }),
      new webpack.optimize.DedupePlugin(),
      new webpack.DefinePlugin({
        'process.env': { 'NODE_ENV': JSON.stringify(process.env.NODE_ENV) },
      }),
    ],
  };
  deps.forEach((dep) => {
    const depPath = path.resolve(nodeModulesDir, dep);
    config.resolve.alias[dep.split(path.sep)[0]] = depPath;
    config.module.noParse.push(depPath);
  });
  module.exports = merge(common, config);
}

1
没有答案来解决这个错误吗? - super1ha1
2个回答

2

当我开始使用babel-loader > 6时,我遇到了同样的问题。

通过在webpack dev服务器配置中添加contentBase解决了这个问题。

在我的情况下,它看起来像这样:

new WebPackDevServer(webpack(config), {
    publicPath: config.output.publicPath,
    hot: true,
    historyApiFallback: true,
    contentBase: __dirname + '/public'
}).listen(3000, 'localhost')

1
我很想看到你的webpack配置文件以确定问题所在,但从错误信息来看,可能存在多个问题:
  • 确保您使用正确的端口
  • 确保您的webpack配置已设置路径公共路径(public path)
  • 确保您也设置了contentBase

没有看到您的webpack文件和更具体的细节,很难确定问题所在。但您可以随时访问https://webpack.github.io/docs/webpack-dev-server.html获取有关如何设置它的信息。


嗨,感谢您的回复,我已经更新了webpack配置文件! - super1ha1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接