platform-checker

An ES module for checking which "computing platform" the script is executed in. For now it can check if it's running in Node.js, Deno or the browser.

Usage no npm install needed!

<script type="module">
  import platformChecker from 'https://cdn.skypack.dev/platform-checker';
</script>

README

platform-checker

Description

An ES module for checking which computing platform the script is executed in. For now it can check if it's running in Node.js, Deno or the browser.

Example

import {platform, platformVersion} from 'platform-checker'

console.log(platform, platformVersion)

Example output:

Platforn Output
Browser Browser Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36
Deno Deno 1.8.3
Node.js Node.js v15.13.0

How to use

Install using NPM

npm i platform-checker

Import the ES module into Node.js

import {platform} from 'platform-checker'

Got problems using ES modules? Click here or read this.

Import the ES module into the browser or Deno

import {platform} from '/node_modules/platform-checker/source/platformChecker.js'

Cumbersome? Consider using import maps so you can import it just like in Node.js. Also see the Deno specific documentation for import maps if using Deno.

Funding

If you find this useful then please consider helping me out (I'm jobless and sick). For more information visit my GitHub sponsors page, my profile or my simple website.

Source code

Here is the full source code just to demonstrate how this works:

export const nodejs = (globalThis.Buffer && globalThis.process) ? process.version : undefined
export const deno = globalThis.Deno?.version?.deno
export const browser = globalThis.navigator?.userAgent

export const platform = (()=>{
  if (deno) return 'Deno'
  if (browser) return 'Browser'
  if (nodejs) return 'Node.js'
})()
export const platformVersion = deno || browser || nodejs

Have a happy day! ❤️