Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Easily detect what host environment your code is running in

License

NotificationsYou must be signed in to change notification settings

JS-DevTools/host-environment

Repository files navigation

Easily detect what host environment your code is running in

npmLicenseBuy us a tree

Build StatusCoverage StatusDependencies

OS and Browser Compatibility

Host Environment is auniversal JavaScript library that makes it easy to detect what host environment your code is running in. It lets you answer questions like:

  • 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?
  • Are we running in a CI/CD environment?

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 apolyfill library. Or maybe you need toconditionally run tests in different environments. Whatever your reason, Host Environment can help.

Example

importhostfrom"@jsdevtools/host-environment";if(host.browser){// Browser logic hereif(host.browser.IE){// Load a polyfill for Internet Explorer}}if(host.node){// Node.js logic hereif(host.node.version<8){// Load a polyfill for older versions of Node}if(host.os.windows){// Windows-specific logic here}}

Related Projects

Installation

Install usingnpm:

npm install @jsdevtools/host-environment

Usage

When using Host Environment in Node.js apps, you'll probably want to useCommonJS syntax:

consthost=require("@jsdevtools/host-environment");

When using a transpiler such asBabel orTypeScript, or a bundler such asWebpack orRollup, you can useECMAScript modules syntax instead:

importhostfrom"@jsdevtools/host-environment";

Browser support

Host Environment supports recent versions of every major web browser. Older browsers may requireBabel and/orpolyfills.

To use Host Environment in a browser, you'll need to use a bundling tool such asWebpack,Rollup,Parcel, orBrowserify. Some bundlers may require a bit of configuration, such as settingbrowser: true inrollup-plugin-resolve.

API

host.global

When running in a web browser,host.global is a reference to thewindow object. When running in Node.js, it's a reference to theglobal object.

host.path

The path of the host process, as a string. When running in Node.js, this is set toprocess.execPath. When running in a web browser, it is the URL of the web page (window.location.href).

host.url

The same ashost.path, but as a parsedURL object. When running in Node.js, this will be afile:// URL.

host.cwd

The current working directory, as a string. When running in Node.js, this is set toprocess.cwd. When running in a web browser, it is the parent directory of the current web page

host.cwdURL

The same ashost.cwd, but as a parsedURL object. When running in Node.js, this will be afile:// URL.

host.os

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: Onlyone of the properties will betrue. All others arefalse.

host.env

This property is an object containing environment variables as key/value strings. When running in Node.js, it is set toprocess.env.

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 likekarma-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'}

host.ci

When running on aknown CI/CD host, this property is an object containing informaation about the CI/CD host and the job that's being run.

When running outside of a CI/CD host, this property isfalse. Since CI/CD hosts are detected based on the presence of environment variables, this property will usually befalse when running in a web browser (where environment variables aren't accessible). However, when paired with tools likekarma-host-environment, it's possible to work-around this limitation and allow you to access CI/CD information in the browser.

{name:"Travis CI",// The name of the CI/CD hostTRAVIS:true,// The CI/CD host as a booleanpr:true,// Whether the CI/CD job was triggered by a pull-request}

host.node

This property isfalse 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:7.3,// 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}

host.browser

This property isfalse 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:58.4,// 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 mobile},firefox: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: Onlyone of the browser properties will be an object. All others arefalse.

Contributing

Contributions, enhancements, and bug-fixes are welcome!Open an issue on GitHub andsubmit a pull request.

Building

To build the project locally on your computer:

  1. Clone this repo
    git clone https://github.com/JS-DevTools/host-environment.git

  2. Install dependencies
    npm install

  3. Build the code
    npm run build

  4. Run the tests
    npm test

License

Host Environment is 100% free and open-source, under theMIT license. Use it however you want.

This package isTreeware. If you use it in production, then we ask that youbuy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Big Thanks To

Thanks to these awesome companies for their support of Open Source developers ❤

Travis CISauceLabsCoveralls


[8]ページ先頭

©2009-2025 Movatter.jp