如何捆绑"表达" node.js应用程序? (使用Webpack)

时间:2016-08-17 01:50:56

标签: node.js express webpack

我试图将node.js表示应用程序捆绑到单个文件中以便分发以删除服务器。我正在尝试将webpack用于此类目的。捆绑过程工作正常,但当我尝试从捆绑包运行应用程序时,我收到错误:

  

错误:此浏览器不支持安全随机数生成   使用chrome,FireFox或Internet Explorer 11 "

下面是我的webpack配置。我正在使用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)
  ]
}

这是一个完整的错误堆栈:

    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)

2 个答案:

答案 0 :(得分:1)

我确定@tomitrescak从4年前就已经找到了这个问题的答案...但是,对于在这里绊脚石的其他人- webpack 配置中缺少一个选项:< / p>

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

希望,这将对某人有所帮助...

答案 1 :(得分:0)

游戏后期但crypto被浏览器替换替换的事实是构建失败的原因。我通过简单地从构建中排除该模块解决了浏览器的这个问题,因为意图是使用NodeJS运行它,加密模块将按预期需要:

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

对于Webpack,不包括加密也应该有效。