[bug] global is not defined
Note: for support questions, please use one of these channels: stackoverflow or slack
You want to:
- [x] report a *bug*
- [ ] request a *feature*
Current behaviour
socket.io-client doesn't work with @angular/[email protected] because of a dependency on node.js' global var
(seems to come from [email protected], but I can't find who maintains that versions)
Steps to reproduce (if the current behaviour is a bug)
see https://github.com/angular/angular-cli/issues/8160
Expected behaviour
Setup
- OS:
- browser:
- socket.io version:
2.0.4
Also facing this issue currently
Since https://github.com/angular/angular-cli/issues/8160 is now resolved, can we close this issue? Thanks!
I don't think so, because in the angular/cli > 6 they permanently removed global. Can you please start looking intoit again? https://github.com/angular/angular-cli/issues/9827
BR, Kevin
A workaround until Angular CLI maybe fixes it or make it possible to override the node command, then you can add the socket.io-client slim file in the paths section of the tsconfig.app.json
"paths": {
....
"socket.io-client": ["../node_modules/socket.io-client/dist/socket.io.slim.js"]
}
Even if it does currently work, according to https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814, this will be an issue in Angular@6:
"[this] just generally make for a situation where browser code that shouldn't work at all works only when built in with very specific tooling. This is not a good situation. "
I confirm it's broken in Angular 6 RC5. Does anybody know a workaround?
@ngervasi we have done it by doing like i commented here: https://github.com/socketio/socket.io-client/issues/1166#issuecomment-379225639
I tried:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"module": "es2015",
"baseUrl": "",
"types": []
},
"exclude": [
"test.ts",
"**/*.spec.ts"
],
"paths": {
"socket.io-client": ["../node_modules/socket.io-client/dist/socket.io.slim.js"]
}
}
but it's not working in Angular 6RC5, I still get the error:
is-buffer.js:4 Uncaught ReferenceError: global is not defined
at Object../node_modules/socket.io-client/node_modules/socket.io-parser/is-buffer.js (is-buffer.js:4)
at __webpack_require__ (bootstrap:74)
at Object../node_modules/socket.io-client/node_modules/socket.io-parser/binary.js (binary.js:8)
at __webpack_require__ (bootstrap:74)
at Object../node_modules/socket.io-client/node_modules/socket.io-parser/index.js (index.js:8)
at __webpack_require__ (bootstrap:74)
at Object../node_modules/socket.io-client/lib/index.js (index.js:7)
at __webpack_require__ (bootstrap:74)
at Object../src/app/_services/websocket.service.ts (log.service.ts:5)
at __webpack_require__ (bootstrap:74)
According to https://github.com/angular/angular-cli/issues/8160#issuecomment-386153833:
adding the following polyfills.ts should work in Angular@6:
// Add global to window, assigning the value of window itself.
(window as any).global = window;
In any case, this issue should not be resolved IMO, socket.io-client should either remove the implicit dependency on global, or document that a polyfill is needed (by anyone using the pre-bundled sources, not just Angular users)
(BTW where are the sources for latest [email protected]? maybe I could help)
Which is the better fix? https://github.com/socketio/socket.io-client/issues/1166#issuecomment-379225639 (adding to paths in tsconfig) or https://github.com/socketio/socket.io-client/issues/1166#issuecomment-386195105 (assigning to global)?
Both options worked for me in Angular v6 project generated with Angular CLI.
@amitport the sources for the has-binary2 package are here: https://github.com/darrachequesne/has-binary
Though I'm afraid it's not the sole occurence.
@darrenmothersele don't think there is exactly one "better". Adding 'global' may have side effects for other libs while adding the 'slim' file may have a negative effect on bundle size (not necessarily)
@darrachequesne added a PR https://github.com/darrachequesne/has-binary/pull/4 (it's a start :))
I tried both solutions and with a current angular 6.0.0 project and none works. I added the paths entry to the tsconfig.app.json and the entry to polyfills.ts but still getting the same error in chrome.
same here, the tsconfig solution does not work on ng cli 6
// Add global to window, assigning the value of window itself. (window as any).global = window;
is working for me Thanks.
Works for me, too! Thanks!
It works for me too ! Thanks a lot ! :D
where did you put (window as any).global = window; ?
Thanks.
Hello,
Just right there (on the picture).
2018-05-17 21:07 GMT+02:00 Alberto Basaglia [email protected]:
where did you put (window as any).global = window; ? Thanks.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/socketio/socket.io-client/issues/1166#issuecomment-389975067, or mute the thread https://github.com/notifications/unsubscribe-auth/APZTlGpMwMpnAscifHT2ZQrRUSJqLejCks5tzcp3gaJpZM4QD_kq .
--
Arnaud Tussy-Vassilieff Développeur web full-stack chez Leaf Websites
Site web : www.leaf-website.esy.es http://www.leaf-website.esy.es Mail : [email protected] [email protected] Tél : 07 77 83 29 44
where did you put...
Put this in your polyfills.ts
(window as any).global = window