64

I have jasmine test spec file and I want to run it using both node.js and in browser. How can I detect if script is running in node?

4
  • 1
    stackoverflow.com/questions/17575790/… Commented Dec 31, 2015 at 19:36
  • use this npmjs.com/package/detect-is-node Commented Jul 31, 2016 at 6:22
  • @CiroSantilli郝海东冠状病六四事件法轮功 it was asked 5 years ago but I think this is better answer, the solution looks beter. Commented Jul 14, 2020 at 6:18
  • Hi there, date does not matter much BTW, I just go by question upvotes since it is so hard to decide answer quality. Answers can be migrated if not covered on the other question. Commented Jul 14, 2020 at 6:59

3 Answers 3

89

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
Sign up to request clarification or add additional context in comments.

6 Comments

I found that many proposed solutions stop working if the code has been webpack'd, because webpack defines all kinds of process, module, require, etc symbols. window usually still works, unless of course you additionally also want to detect being run from a webworker, where there is no window.
the check on window will not worker inside a WebWorker ... :) however, the check against process will work canonically in all environments afaiu
another idea, to avoid checking on process in the browser: the global var self is an alternative to using window (but is equivalent to it in main thread) and refers to the global worker scope otherwise.
example: if (typeof self === 'undefined') { /* neither web window nor web worker */ }
As of 2021, Firefox browser has an object on the root called process, so the second method doesn't work unless you also check for the title attribute like this; typeof process === 'object' && process.title === 'node'
|
28

There is an npm package just for this and it can be used both on client-side and server-side.

browser-or-node

You can use it in your code like this

import { isBrowser, isNode } from 'browser-or-node';

if (isBrowser) {
  // do browser only stuff
}

if (isNode) {
  // do node.js only stuff
}

Disclaimer: I am the author of this package :)

2 Comments

the function in this package does the best job possible +1
Tried this but it didn't work for me. Running my app through node / npm still returned isNode = false :( unless I misunderstood?
-2

You check for the exports variable, like this:

if (typeof exports === 'object') {
    // Node. Does not work with strict CommonJS, but
    // only CommonJS-like environments that support module.exports,
    // like Node.
    module.exports = { ... };
}

1 Comment

For this to work, you'll need to make sure you don't have window.exports set in the browser, or else this duck-typing will fail.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.