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?
-
1stackoverflow.com/questions/17575790/…PeterVC– PeterVC2015-12-31 19:36:14 +00:00Commented Dec 31, 2015 at 19:36
-
use this npmjs.com/package/detect-is-nodeabhirathore2006– abhirathore20062016-07-31 06:22:07 +00:00Commented Jul 31, 2016 at 6:22
-
@CiroSantilli郝海东冠状病六四事件法轮功 it was asked 5 years ago but I think this is better answer, the solution looks beter.jcubic– jcubic2020-07-14 06:18:54 +00:00Commented 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.Ciro Santilli OurBigBook.com– Ciro Santilli OurBigBook.com2020-07-14 06:59:50 +00:00Commented Jul 14, 2020 at 6:59
Add a comment
|
3 Answers
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
6 Comments
jlh
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.Tchakabam
the check on
window will not worker inside a WebWorker ... :) however, the check against process will work canonically in all environments afaiuTchakabam
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.Tchakabam
example:
if (typeof self === 'undefined') { /* neither web window nor web worker */ }Jivings
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' |
There is an npm package just for this and it can be used both on client-side and server-side.
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
Tchakabam
the function in this package does the best job possible +1
levraininjaneer
Tried this but it didn't work for me. Running my app through node / npm still returned isNode = false :( unless I misunderstood?
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
Josh Beam
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.