tomitrescak
tomitrescak

Reputation: 1110

How to bundle "express" node.js application? (with Webpack)

I'm trying to bundle a node.js express application into single file for distribution to remove servers. I am trying to use webpack for such purposes. The bundling process works fine, but when I try to run the app from the bundle I am getting the error:

Error: secure random number generation not supported by this browser use chrome, FireFox or Internet Explorer 11"

Below is my webpack config. I am running the code with node bundle.js

var webpack = require('webpack');

module.exports = {
  context: __dirname,
  devtool: 'eval',
  entry: [
    './index.js'
  ],
  output: {
    filename: 'bundle.js',
    publicPath: '/public'
  },
  resolve: {
    extensions: ['', '.js', '.json'],
  },
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: 'null-loader'
      },
      {
        test: /\.json$/,
        loader: 'null-loader'
      }
    ]
  },
  plugins: [
    new webpack.DefinePlugin({
     'process.env.NODE_ENV': JSON.stringify('production')
    }),
    new webpack.NoErrorsPlugin(),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.UglifyJsPlugin({
      compress: { warnings: false }
    }),
    new webpack.optimize.AggressiveMergingPlugin(),
    new webpack.optimize.OccurenceOrderPlugin(true)
  ]
}

Here is a full error stack:

    Error: secure random number generation not supported by this browser
use chrome, FireFox or Internet Explorer 11
    at module.exports (webpack:///./~/crypto-browserify/rng.js?:21:13)
    at exports.randomBytes (webpack:///./~/crypto-browserify/index.js?:22:23)
    at _rng (webpack:///./~/node-uuid/uuid.js?:60:53)
    at eval (webpack:///./~/node-uuid/uuid.js?:121:20)
    at Object.eval (webpack:///./~/node-uuid/uuid.js?:272:3)
    at eval (webpack:///./~/node-uuid/uuid.js?:274:30)
    at Object.<anonymous> (/Users/tomi/Github/apps/react-app-test/server/bundle.js:545:2)
    at __webpack_require__ (/Users/tomi/Github/apps/react-app-test/server/bundle.js:20:30)
    at eval (webpack:///./~/graphql-tools/dist/mock.js?:18:17)
    at Object.<anonymous> (/Users/tomi/Github/apps/react-app-test/server/bundle.js:341:2)

Upvotes: 5

Views: 5471

Answers (2)

Jonan Gueorguiev
Jonan Gueorguiev

Reputation: 1206

I'm sure @tomitrescak has already found the answer to this question from 4 years ago... but, for anybody else stumbling here - there is one option missing in the webpack config:

  ...
  target: 'node',
  ...

Hope, this will help somebody...

Upvotes: 6

nfroidure
nfroidure

Reputation: 1601

Late in the game but the fact that crypto is replaced by the browser substitute is the reason why the build is failing. I solved this issue with browserify by simply excluding that module from the build since the intent is to run it with NodeJS, the crypto module will just be required as expected:

browserify index.js --exclude crypto  -o build.js

For Webpack, excluding crypto should also work then.

Upvotes: 1

Related Questions