Skip to content

Runtime error when node.global is set to false. #1147

Closed
@filipesilva

Description

@filipesilva
  • Operating System: Windows 10
  • Node Version: 6.11.1
  • NPM Version: 5.3.0
  • webpack Version: 3.7.1
  • webpack-dev-server Version: 2.9.1
  • This is a bug
  • This is a feature request
  • This is a modification request

Code

A reproduction can be found at https://github.com/filipesilva/webpack-dev-server-global.

  // webpack.config.js
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');


module.exports = {
  entry: './src/main.js',
  devtool: 'source-map',
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'bundle.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html'
    })
  ],
  node: {
    global: false
  }
};
  // main.js
console.log('hello');

Expected Behavior

Using webpack-dev-server on a browser app with node.global = false should not break at runtime.

Actual Behavior

webpack-dev-server has a runtime error:

Uncaught ReferenceError: global is not defined
    at Object.<anonymous> (browser-crypto.js:3)
    at __webpack_require__ (bootstrap 46c9e79f3cb14d538cb2:19)
    at Object.<anonymous> (random.js:4)
    at __webpack_require__ (bootstrap 46c9e79f3cb14d538cb2:19)
    at Object.<anonymous> (event.js:3)
    at __webpack_require__ (bootstrap 46c9e79f3cb14d538cb2:19)
    at Object.<anonymous> (websocket.js:3)
    at Object.<anonymous> (websocket.js:99)
    at __webpack_require__ (bootstrap 46c9e79f3cb14d538cb2:19)
    at Object.<anonymous> (transport-list.js:5)

This happens because webpack-dev-server is bundling sockjs-client from source, and sockjs-client sources assume a node environment with global available.

For more details about how this behaviour is problematic, see angular/angular-cli#5804, mapbox/mapbox-gl-js#4359 (comment) and sockjs/sockjs-client#401 (comment).

For Bugs; How can we reproduce the behavior?

git clone  https://github.com/filipesilva/webpack-dev-server-global
cd webpack-dev-server-global
npm install
npm run serve

For Features; What is the motivation and/or use-case for the feature?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions