Coder Social home page Coder Social logo

Error: The 'server' compiler must export a function in the form of `(options) => (req, res, next) => void` about webpack-hot-server-middleware HOT 3 CLOSED

60frames avatar 60frames commented on September 24, 2024
Error: The 'server' compiler must export a function in the form of `(options) => (req, res, next) => void`

from webpack-hot-server-middleware.

Comments (3)

hrasoa avatar hrasoa commented on September 24, 2024 7

Fixed, following the example code for the server webpack config part I added:

  output: {
    path: resolve(__dirname, 'public'),
    filename: 'server.js',
    libraryTarget: 'commonjs2'
  }

from webpack-hot-server-middleware.

richardscarrott avatar richardscarrott commented on September 24, 2024

Your server entry looks fine, could you share your webpack config and middleware calling code?

from webpack-hot-server-middleware.

hrasoa avatar hrasoa commented on September 24, 2024

webpack.config.js:

const webpack = require('webpack');
const { resolve } = require('path');

module.exports = [{
  name: 'client',
  target: 'web',
  entry: [
    'react-hot-loader/patch',
    'webpack-hot-middleware/client',
    'webpack/hot/only-dev-server',
    './src/index.js'
  ],
  output: {
    path: resolve(__dirname, 'public'),
    filename: 'bundle.js',
    publicPath: '/'
  },
  devtool: 'inline-source-map',
  devServer: {
    hot: true,
    publicPath: '/',
    contentBase: resolve(__dirname, 'public')
  },
  module: {
    loaders: [
      {
        test: /\.json$/,
        loader: 'json-loader',
        include: /src/
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: /src/
      }
    ]
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin(),
    new webpack.NoEmitOnErrorsPlugin()
  ]
}, {
  name: 'server',
  target: 'node',
  entry: './server.js',
  module: {
    loaders: [
      {
        test: /\.json$/,
        loader: 'json-loader',
        exclude: /node_modules/
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      }
    ]
  }
}
];

index.js:

import config from './config';
import apiRouter from './src/api/index';
import path from 'path';
import express from 'express';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import webpackHotServerMiddleware from 'webpack-hot-server-middleware';
import webpackConfig from './webpack.config';
import webpack from 'webpack';

const bundler = webpack(webpackConfig);
const app = express();

app.use(webpackDevMiddleware(bundler, {
  publicPath: '/',
  stats: 'minimal'
}));
app.use(webpackHotMiddleware(bundler.compilers.find(compiler => compiler.name === 'client')));
app.use(webpackHotServerMiddleware(bundler));

app.use('/api', apiRouter);
app.use(express.static('public'));

app.listen(config.port, config.host, () => {
  console.info('Express listening on port', config.port);
});

from webpack-hot-server-middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.