Error: Cannot find module ‘stream’

My webpack.production.backend.js file;

var webpack = require('webpack'),
    path = require('path'),
    fs = require('fs'),
    ExtractTextPlugin = require("extract-text-webpack-plugin");

var nodeModules = {};
fs.readdirSync('node_modules')
  .filter(function(x) {
    return ['.bin'].indexOf(x) === -1;
  })
  .forEach(function(mod) {
    nodeModules[mod] = 'commonjs ' + mod;
  });

module.exports = {
  devtool: 'sourcemap',
  entry: {
    server: './server.babel.js'
  },
  externals: nodeModules,
  module: {
    loaders: [
      {test: /\.json$/, loader: 'json-loader'},
      {test: /\.txt$/, loader: 'raw-loader'},
      {test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/, loader: 'url-loader?limit=10000'},
      {test: /\.(eot|ttf|wav|mp3)$/, loader: 'file-loader'},
      {
        test: /\.jsx?$/,
        loaders: ['babel']
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
      },
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass-loader')
      }
    ]
  },
  node: {
    fs: "empty",
    stream: "empty",
    __filename: true,
    __dirname: true,
    process: true
  },
  output: {
    path: path.join(__dirname, './.build'),
    filename: '[name].js'
  },
  plugins: [
    new webpack.NoErrorsPlugin(),
    new webpack.optimize.UglifyJsPlugin({compressor: {warnings: false}}),
    new webpack.optimize.LimitChunkCountPlugin({maxChunks: 1}),
    new ExtractTextPlugin('bundle.css', {
      allChunks: true
    })
  ],
  resolve: {
    root: [
      path.join(__dirname),
      path.join(__dirname, "node_modules"),
    ],
    extensions: ['', '.js', '.json'],
  },
  target: 'node'
};

When I start my application:

Error: Cannot find module 'stream'

Author: Fantashit

3 thoughts on “Error: Cannot find module ‘stream’

  1. I am having the same issue when I try to deploy my react app onto Heroku. I tried doing the above, clearing the cache and reinstalling node but still no luck. Any other suggestions?

  2. Did you have any luck resolving the issue? I’m facing the same issue. My app works, but after bundling with webpack it started throwing the same error.

Comments are closed.