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

Cannot read property 'WebSocket' of undefined #584

Closed
1 of 2 tasks
soyjuanmedina opened this issue Jun 4, 2019 · 8 comments
Closed
1 of 2 tasks

Cannot read property 'WebSocket' of undefined #584

soyjuanmedina opened this issue Jun 4, 2019 · 8 comments

Comments

@soyjuanmedina
Copy link

Note: for support questions, please use one of these channels: stackoverflow or slack

You want to:

  • report a bug
  • request a feature

Current behaviour

After update my Angular App to Angular 8 I can npm start and npm build, but when I upoload to the server the console of the browser yield
Cannot read property 'WebSocket' of undefined

Other information (e.g. stacktraces, related issues, suggestions how to fix)

I really don't know if the issue is for this library, but I read the problem is about the
global.WebSocket || global.MozWebSocket;
webpack/webpack-dev-server#174 (comment)
in some libraries and I use yours

@soyjuanmedina
Copy link
Author

I really don't know who is guilty, but I supousse it's not your (but I can't delete the issue).

I solve it changing compilerOptions in tsconfig.json from 'es2015' to 'es5'

Thanks and sorry if not your fault.

@MickL
Copy link

MickL commented Jul 17, 2019

I have the same problem when using target es2015 in tsconfig.

I wonder why this error occurs because it is an important new feature of Angular 8 to compile modern js AND older js seperate if defined in browserlist. But only if you set to es2015 so it should not be changed back to es5.

@MickL
Copy link

MickL commented Jul 17, 2019

@soyjuanmedina Could you please reopen?

@NicolasKritter
Copy link

@MickL

I think the more correct way to get window is Function('return this')() instead. The content in Function constructor is non-strict regardless of the environment it is created in.

Originally posted by @andy0130tw in webpack/webpack-dev-server#174 (comment)

I had this problem after an upgrade to angular 8.1 and it works perfectly after doing what he suggested.
I made a small patch.js to run after npm install in my package.json

const { readFileSync, writeFileSync,existsSync } = require('fs');
const patchIdentifier = '//-patched';

const fileToPatch = './node_modules/websocket/lib/browser.js';
const codeToPatch= 'var _global = (function() { return this; })();';
const patchCode = "var _global = Function('return this')();" + patchIdentifier;

function doPatch(fileName,sourceCode) {
  if (!existsSync(fileName)) {
    console.log('file not found '+fileName)
    process.exit(0);
  }
  const contents = readFileSync(fileName).toString().split('\n');
  // Check if code has been patched already
  const hasBeenPatched = contents.find(line => line.indexOf(patchIdentifier) !== -1);
  
  if (!hasBeenPatched) {
    const lineNumber = contents.findIndex(line => line.indexOf(sourceCode) !== -1);
    if (lineNumber < 0) {
      console.error('Could not find source code. Please check ' + fileName + ' and update the patch accordingly');
      process.exit(1);
    }
    // replace the line
    contents.splice(lineNumber, 1, patchCode);
    const updatedContents = contents.join('\n');
    writeFileSync(fileName, updatedContents);

    console.log('websocket browser.js has been Monkey patched');
  } else {
    console.log('websocket browser.js has already been patched');
  }
}
doPatch(fileToPatch,codeToPatch);
process.exit(0);

PS: with this solution, I could keep the es2015 in my tsconfig

@loveneetabsolvetech
Copy link

Annotation 2019-12-31 133701
Just need to update your opentok.js file as per screenshot

@MickL
Copy link

MickL commented Jan 23, 2020

@loveneetabsolvetech Could you create a pull request? Or maybe someone of the contributors could fix this issue?

@darrachequesne
Copy link
Member

I think this should be fixed by socketio/engine.io-client@357f01d (included in 3.4.1).

@Chingshangkhomba
Copy link

Annotation 2019-12-31 133701 Just need to update your opentok.js file as per screenshot

this works

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

No branches or pull requests

6 participants