window is not defined angular universal third library
I am working with the library ng2-mqtt and I used it im my component like this:
import 'ng2-mqtt/mqttws31.js'; declare var Paho: any;
Now I am getting following error:
ReferenceError: window is not defined at Object.<anonymous> (/Users/Picchu/Documents/em3/node_modules/ng2-mqtt/mqttws31.js:2143:4) at Module._compile (module.js:556:32) at Object.Module._extensions..js (module.js:565:10) at Module.load (module.js:473:32) at tryModuleLoad (module.js:432:12) at Function.Module._load (module.js:424:3) at Module.require (module.js:483:17) at require (internal/module.js:20:19) at Object.<anonymous> (/Users/Picchu/Documents/em3/dist/server.js:18707:18)
How can I fix this issue?
windowshouldn't be used in universal applications on server side, because Node.js doesn't have
window, and having a dummy
global.windowcurrently affects the way Angular detects global variable.
If the package uses
window, an issue can be opened in its repository and/or it can be forked and changed to not use
Since packages that rely on
windowoften rely on things that are specific to client side, they won't work as expected on server side even if this issue is sorted out.
Package description says:
Depends on the library from: https://eclipse.org/paho/clients/js/
While library description says:
Usually third-party Angular module that isn't supposed to work as expected on server side should be stubbed or mocked; dummy module with fake directives and services is imported in
app.server.tsinstead of real module.