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

ReferenceError: navigator is not defined in angular universal #13

Open
mkhalesi opened this issue Aug 24, 2021 · 2 comments
Open

ReferenceError: navigator is not defined in angular universal #13

mkhalesi opened this issue Aug 24, 2021 · 2 comments

Comments

@mkhalesi
Copy link

hi
when run project in ssr
with command : npm run dev:ssr
get same error:

return new DeviceUUID(navigator.userAgent);
                        ^
ReferenceError: navigator is not defined

im using a mock browser , in server.ts file :

const MockBrowser = require('mock-browser').mocks.MockBrowser;
const mock = new MockBrowser();
global['navigator'] = mock.getNavigator();
global['window'] = mock.getWindow();
global['document'] = mock.getDocument();

thanks

@mkhalesi mkhalesi reopened this Aug 24, 2021
@Parziphal
Copy link

SSR runs on the server, where navigator and other objects (like window) aren't available. When coding for SSR you have to make sure to check the platform before executing code that use these objects.

@mkhalesi
Copy link
Author

mkhalesi commented Oct 4, 2021

@Parziphal
i worked based on your tips
like this:

 if (isPlatformBrowser(this.platformId)) {
        import('device-uuid').then( module => {
             const deviceId = new module.DeviceUUID().get() ; 
         });  
  }

and worked properly
thanks :)

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

2 participants