In Vue.js how can I detect if code in created() hook runs in the browser? [duplicate]












2
















This question already has an answer here:




  • Environment detection: node.js or browser

    4 answers




I have a Vue SSR-application and for some components, I need to know whether they run on Node during server-side rendering or whether they run on the browser.



I've set the process env in the Webpack config like



process.env.VUE_ENV === 'server' 


which works. But for various reasons, I need a detection independent of the built environment.



I'd like to check for the browser/node in the created() hook.



How would I do that?










share|improve this question















marked as duplicate by thanksd vue.js
Users with the  vue.js badge can single-handedly close vue.js questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:48


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    You can try to access a browser specific object (like navigator object) within your created() hook - and catch the error it throws when trying to access this object on nodejs fails

    – Mortz
    Nov 21 '18 at 10:37











  • stackoverflow.com/questions/4224606/…

    – ssc-hrep3
    Nov 21 '18 at 10:38
















2
















This question already has an answer here:




  • Environment detection: node.js or browser

    4 answers




I have a Vue SSR-application and for some components, I need to know whether they run on Node during server-side rendering or whether they run on the browser.



I've set the process env in the Webpack config like



process.env.VUE_ENV === 'server' 


which works. But for various reasons, I need a detection independent of the built environment.



I'd like to check for the browser/node in the created() hook.



How would I do that?










share|improve this question















marked as duplicate by thanksd vue.js
Users with the  vue.js badge can single-handedly close vue.js questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:48


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    You can try to access a browser specific object (like navigator object) within your created() hook - and catch the error it throws when trying to access this object on nodejs fails

    – Mortz
    Nov 21 '18 at 10:37











  • stackoverflow.com/questions/4224606/…

    – ssc-hrep3
    Nov 21 '18 at 10:38














2












2








2









This question already has an answer here:




  • Environment detection: node.js or browser

    4 answers




I have a Vue SSR-application and for some components, I need to know whether they run on Node during server-side rendering or whether they run on the browser.



I've set the process env in the Webpack config like



process.env.VUE_ENV === 'server' 


which works. But for various reasons, I need a detection independent of the built environment.



I'd like to check for the browser/node in the created() hook.



How would I do that?










share|improve this question

















This question already has an answer here:




  • Environment detection: node.js or browser

    4 answers




I have a Vue SSR-application and for some components, I need to know whether they run on Node during server-side rendering or whether they run on the browser.



I've set the process env in the Webpack config like



process.env.VUE_ENV === 'server' 


which works. But for various reasons, I need a detection independent of the built environment.



I'd like to check for the browser/node in the created() hook.



How would I do that?





This question already has an answer here:




  • Environment detection: node.js or browser

    4 answers








javascript vue.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 10:23









Yashwardhan Pauranik

2,03111529




2,03111529










asked Nov 21 '18 at 10:14









LongHikeLongHike

7651123




7651123




marked as duplicate by thanksd vue.js
Users with the  vue.js badge can single-handedly close vue.js questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:48


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by thanksd vue.js
Users with the  vue.js badge can single-handedly close vue.js questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:48


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    You can try to access a browser specific object (like navigator object) within your created() hook - and catch the error it throws when trying to access this object on nodejs fails

    – Mortz
    Nov 21 '18 at 10:37











  • stackoverflow.com/questions/4224606/…

    – ssc-hrep3
    Nov 21 '18 at 10:38














  • 1





    You can try to access a browser specific object (like navigator object) within your created() hook - and catch the error it throws when trying to access this object on nodejs fails

    – Mortz
    Nov 21 '18 at 10:37











  • stackoverflow.com/questions/4224606/…

    – ssc-hrep3
    Nov 21 '18 at 10:38








1




1





You can try to access a browser specific object (like navigator object) within your created() hook - and catch the error it throws when trying to access this object on nodejs fails

– Mortz
Nov 21 '18 at 10:37





You can try to access a browser specific object (like navigator object) within your created() hook - and catch the error it throws when trying to access this object on nodejs fails

– Mortz
Nov 21 '18 at 10:37













stackoverflow.com/questions/4224606/…

– ssc-hrep3
Nov 21 '18 at 10:38





stackoverflow.com/questions/4224606/…

– ssc-hrep3
Nov 21 '18 at 10:38












1 Answer
1






active

oldest

votes


















1














I took the following line -verbatim- from the Vue.js source code ..



const inBrowser = typeof window !== 'undefined';



You can use it to verify if your code is running in the browser.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I took the following line -verbatim- from the Vue.js source code ..



    const inBrowser = typeof window !== 'undefined';



    You can use it to verify if your code is running in the browser.






    share|improve this answer




























      1














      I took the following line -verbatim- from the Vue.js source code ..



      const inBrowser = typeof window !== 'undefined';



      You can use it to verify if your code is running in the browser.






      share|improve this answer


























        1












        1








        1







        I took the following line -verbatim- from the Vue.js source code ..



        const inBrowser = typeof window !== 'undefined';



        You can use it to verify if your code is running in the browser.






        share|improve this answer













        I took the following line -verbatim- from the Vue.js source code ..



        const inBrowser = typeof window !== 'undefined';



        You can use it to verify if your code is running in the browser.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 12:06









        Husam IbrahimHusam Ibrahim

        3,323417




        3,323417