How to detect if script is running in browser or in Node.js? [duplicate]

A couple of ideas:

You can check for the window global object, if it is available then you are in a browser

if (typeof window === 'undefined')
// this is node

Or you can check for the process object, if it is available then you are in node

if(typeof process === 'object')
// this is also node

Leave a Comment