Easily detect what host environment your code is running in
- Am I running in Node.js or a web browser?
- Is this Node 8.x or newer?
- Are we in Internet Explorer?
- Is this a Windows computer?
Maybe some parts of your app are only available in Node and not in web browsers. Or maybe you need to determine whether to load a polyfill library. Or maybe you need to conditionally run tests in different environments. Whatever your reason, Host Environment can help.
;if hostbrowser// Browser logic hereif hostbrowserIE// Load a polyfill for Internet Explorerif hostnode// Node.js logic hereif hostnodeversion < 8// Load a polyfill for older versions of Nodeif hostoswindows// Windows-specific logic here
Access environment variables and other system info in your browser tests.
Install using npm:
npm install host-environment
When using Host Environment in Node.js apps, you'll probably want to use CommonJS syntax:
const host = ;
To use Host Environment in a browser, you'll need to use a bundling tool such as Webpack, Rollup, Parcel, or Browserify. Some bundlers may require a bit of configuration, such as setting
browser: true in rollup-plugin-resolve.
When running in a web browser,
host.global is a reference to the
window object. When running in Node.js, it's a reference to the
This property is an object with the following structure:
windows: false // Windows or Windows Phonemac: true // Mac OS or iOSlinux: false // Linux, Android, or other *nix platforms
Note: Only one of the properties will be
true. All others are
This property is an object containing environment variables as key/value strings. When running in Node.js, it is set to
When running in a web browser, it is usually an empty object, since web browsers don't have access to environment variables. However, when paired with tools like karma-host-environment, it's possible to work-around this limitation and allow you to access environment variables in the browser.
TERM: 'xterm-256color'SHELL: '/usr/local/bin/bash'USER: 'maciej'PATH: '~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'PWD: '/Users/maciej'EDITOR: 'vim'SHLVL: '1'HOME: '/Users/maciej'LOGNAME: 'maciej'_: '/usr/local/bin/node'
This property is
false when running in a web browser. When running in Node.js it is an object with the following structure:
v7: true // The major version, as a booleanversion: 73 // The major.minor version, as a floatmajorVersion: 7 // The major version, as an integerminorVersion: 3 // The minor version, as an integerpatchVersion: 24 // The patch version, as an integer
This property is
false when running in Node.js. When running in a browser it is an object with the following structure:
chrome: // false if not Chromev58: true // The major version, as a booleanversion: 584 // The major.minor version, as a floatmajorVersion: 58 // The major version, as an integerminorVersion: 4 // The minor version, as an integerpatchVersion: 3029 // The patch version, as an integermobile: false // true on mobilefirefox: false // An object like above if running in Firefoxsafari: false // An object like above if running in Safariedge: false // An object like above if running in EdgeIE: false // An object like above if running in Internet Explorermobile: false // true for any mobile browser (iOS, Android, Windows Phone, etc)
Note: Only one of the browser properties will be an object. All others are
To build the project locally on your computer:
Clone this repo
git clone https://github.com/JS-DevTools/host-environment.git
Build the code
npm run build
Run the tests
host-environment is 100% free and open-source, under the MIT license. Use it however you want.
Big Thanks To
Thanks to these awesome companies for their support of Open Source developers ❤