Node.js
To detect if your script is running in Node.js:
typeof process === 'object' && process.title === 'node';
jsdom
To detect if your script is running in jsdom:
typeof window === 'object' && window.name === 'nodejs';
Or check the user agent:
typeof navigator === 'object' && navigator.userAgent === 'node.js';
See the issue that inspired this post.