Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 of 3 tasks
filipesilva opened this issue Oct 16, 2017 · 3 comments · Fixed by #1148
Closed
1 of 3 tasks

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

filipesilva opened this issue Oct 16, 2017 · 3 comments · Fixed by #1148

Comments

@filipesilva
Copy link
Contributor

  • 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?

filipesilva added a commit to filipesilva/webpack-dev-server that referenced this issue Oct 16, 2017
`webpack-dev-server` bundles `sockjs-client` from source. `sockjs-client` sources assume a node environment however, and will fail to run if `global` is not available.

By bundling `sockjs-client` from source, the node environment requirement from `sockjs-client` will also be required of the app being served and thus it is not possible to have `node.global = false` in the user app being bundled.

This fix uses the built `sockjs-client` instead of the sources.

Fix webpack#1147
@shellscape
Copy link
Contributor

shellscape commented Oct 16, 2017

Holy hiding edge, batman! I award you 10 points for finding that obscure error. I'd say that first and foremost, that should be addressed with the SockJS team, which it looks like you're well on your way to doing.

I'm focusing my time on the v3 effort and rollout at the moment. Of which one of the major changes is a move away from SockJS and to native WebSocket. I can't speak for all of the contributors of course, but this won't be something I'll be prioritizing for a fix.

Normally I'd suggest that this be closed, citing the move away from SockJS and the open issue tracking the problem with the SockJS project, but I'm good to leave it open for discussion in the event that another contributor or dev finds and submits a fix.

Edit: Ah it seems you've already identified a fix in #1148!

@filipesilva
Copy link
Contributor Author

Thanks for the quick reply @shellscape! I did find a fix but didn't add a test. Will add one now to make sure everything is fine.

filipesilva added a commit to filipesilva/webpack-dev-server that referenced this issue Oct 16, 2017
`webpack-dev-server` bundles `sockjs-client` from source. `sockjs-client` sources assume a node environment however, and will fail to run if `global` is not available.

By bundling `sockjs-client` from source, the node environment requirement from `sockjs-client` will also be required of the app being served and thus it is not possible to have `node.global = false` in the user app being bundled.

This fix uses the built `sockjs-client` instead of the sources.

Fix webpack#1147
@filipesilva
Copy link
Contributor Author

Done, added test.

filipesilva added a commit to filipesilva/webpack-dev-server that referenced this issue Oct 16, 2017
`webpack-dev-server` bundles `sockjs-client` from source. `sockjs-client` sources assume a node environment however, and will fail to run if `global` is not available.

By bundling `sockjs-client` from source, the node environment requirement from `sockjs-client` will also be required of the app being served and thus it is not possible to have `node.global = false` in the user app being bundled.

This fix uses the built `sockjs-client` instead of the sources.

Fix webpack#1147
filipesilva added a commit to filipesilva/webpack-dev-server that referenced this issue Oct 16, 2017
`webpack-dev-server` bundles `sockjs-client` from source. `sockjs-client` sources assume a node environment however, and will fail to run if `global` is not available.

By bundling `sockjs-client` from source, the node environment requirement from `sockjs-client` will also be required of the app being served and thus it is not possible to have `node.global = false` in the user app being bundled.

This fix uses the built `sockjs-client` instead of the sources.

Fix webpack#1147
filipesilva added a commit to filipesilva/webpack-dev-server that referenced this issue Oct 16, 2017
`webpack-dev-server` bundles `sockjs-client` from source. `sockjs-client` sources assume a node environment however, and will fail to run if `global` is not available.

By bundling `sockjs-client` from source, the node environment requirement from `sockjs-client` will also be required of the app being served and thus it is not possible to have `node.global = false` in the user app being bundled.

This fix uses the built `sockjs-client` instead of the sources.

Fix webpack#1147
shellscape pushed a commit that referenced this issue Oct 16, 2017
`webpack-dev-server` bundles `sockjs-client` from source. `sockjs-client` sources assume a node environment however, and will fail to run if `global` is not available.

By bundling `sockjs-client` from source, the node environment requirement from `sockjs-client` will also be required of the app being served and thus it is not possible to have `node.global = false` in the user app being bundled.

This fix uses the built `sockjs-client` instead of the sources.

Fix #1147
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants