├── .gitignore ├── .npmignore ├── .prettierrc ├── .babelrc ├── .eslintrc.js ├── src ├── node.check.ts ├── test.ts └── index.ts ├── .editorconfig ├── readme.md ├── LICENSE └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | dist 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | package-lock.json 4 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "semi": true, 3 | "trailingComma": "all" 4 | } 5 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "cjs": { 4 | "presets": [["@babel/preset-typescript"]], 5 | "plugins": ["@babel/plugin-transform-modules-commonjs"] 6 | }, 7 | "es": { 8 | "presets": [["@babel/preset-typescript"]] 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | ignorePatterns: ["dist/**/*", ".eslintrc.js"], 4 | parser: "@typescript-eslint/parser", 5 | plugins: ["@typescript-eslint", "prettier"], 6 | extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], 7 | rules: { 8 | "prettier/prettier": "error", 9 | }, 10 | }; 11 | -------------------------------------------------------------------------------- /src/node.check.ts: -------------------------------------------------------------------------------- 1 | import { isBrowser, isNode, isJsDom, isDeno } from "./index"; 2 | 3 | console.log(isBrowser ? "❌" : "✅", "browser:", isBrowser); 4 | console.log(isNode ? "✅" : "❌", "node:", isNode); 5 | console.log(isBrowser ? "❌" : "✅", "js-dom:", isJsDom); 6 | console.log(isDeno ? "❌" : "✅", "deno:", isDeno); 7 | 8 | 9 | if (isBrowser || !isNode || isJsDom) { 10 | process.exit(1); 11 | } 12 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # top-most EditorConfig file 2 | root = true 3 | 4 | # Unix-style newlines with a newline ending every file 5 | [*] 6 | end_of_line = lf 7 | insert_final_newline = true 8 | 9 | # Matches multiple files with brace expansion notation 10 | # Set default charset 11 | [*.{js,ts,mjs,cjs}] 12 | charset = utf-8 13 | indent_style = space 14 | indent_size = 2 15 | 16 | # Matches the exact files either package.json 17 | [{package.json}] 18 | indent_style = space 19 | indent_size = 2 20 | 21 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | import isInBrowser, { isBrowser, isDeno, isJsDom, isNode } from "."; 2 | 3 | test("isInBrowser", () => { 4 | // jest runs jsdom, so true 5 | expect(isBrowser).toBe(true); 6 | expect(isInBrowser).toBe(true); 7 | 8 | // jest runs jsdom 9 | expect(isJsDom).toBe(true); 10 | 11 | expect(isBrowser && !isJsDom).toBe(false); 12 | 13 | // jest runs on Node, so true 14 | expect(isNode).toBe(true); 15 | 16 | // jest runs on Deno, so true 17 | expect(isDeno).toBe(true); 18 | 19 | // jest runs JSDOm, so true 20 | }); 21 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export const isJsDom = 2 | typeof navigator === "object" && 3 | navigator.userAgent && 4 | navigator.userAgent.includes("jsdom"); 5 | 6 | export const isNode = 7 | typeof process === "object" && !!process.versions && !!process.versions.node; 8 | 9 | export const isBrowser = 10 | typeof window === "object" && 11 | typeof document === "object" && 12 | document.nodeType === 9; 13 | 14 | export const isDeno = 15 | typeof window === "object" && 16 | typeof window.Deno === "object" 17 | 18 | export default isBrowser; 19 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Is In Browser? 2 | 3 | ```js 4 | import isBrowser from "is-in-browser"; 5 | 6 | if (isBrowser) { 7 | //... 8 | } 9 | ``` 10 | 11 | More thoroughly: 12 | 13 | ```js 14 | import { isJsDom, isNode, isBrowser, isDeno } from "is-in-browser"; 15 | 16 | if (isBrowser) { 17 | // you're in the browser 18 | // jsdom considered in browser 19 | } 20 | 21 | if (isJsDom) { 22 | // you're in the JSDom 23 | } 24 | 25 | if (isNode) { 26 | // you're in the Node 27 | } 28 | 29 | if (isDeno) { 30 | // you're in the Deno 31 | } 32 | ``` 33 | 34 | ## CommonJS 35 | 36 | For those not using Babel / ES6 Modules 37 | 38 | ```js 39 | var isBrowser = require('is-in-browser').default; 40 | 41 | if(isBrowser) { //... } 42 | ``` 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016-2019 Jared Anderson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-in-browser", 3 | "version": "2.0.0", 4 | "description": "Simple check to see if current app is running in browser", 5 | "main": "dist/index.cjs", 6 | "module": "dist/index.mjs", 7 | "types": "dist/index.d.ts", 8 | "exports": { 9 | ".": { 10 | "require": "./dist/index.cjs", 11 | "import": "./dist/index.mjs" 12 | }, 13 | "./package.json": "./package.json" 14 | }, 15 | "scripts": { 16 | "dist.cjs": "BABEL_ENV=cjs babel -x \".ts\" src/index.ts -o dist/index.cjs", 17 | "dist.es": "BABEL_ENV=es babel -x \".ts\" src/index.ts -o dist/index.mjs", 18 | "dist.declarations": "tsc --emitDeclarationOnly src/index.ts --declaration --outDir dist", 19 | "dist": "concurrently \"npm:dist.*\"", 20 | "test.jest": "BABEL_ENV=cjs jest", 21 | "test.node": "BABEL_ENV=cjs babel-node -x \".ts\" src/node.check.ts", 22 | "test": "concurrently \"npm:test.*\"", 23 | "lint": "eslint src --ext .ts", 24 | "format": "prettier -w src", 25 | "prepublishOnly": "concurrently npm:test npm:dist" 26 | }, 27 | "keywords": [], 28 | "browserslist": [ 29 | "defaults" 30 | ], 31 | "license": "MIT", 32 | "devDependencies": { 33 | "@babel/cli": "^7.12.13", 34 | "@babel/core": "^7.12.13", 35 | "@babel/node": "^7.12.13", 36 | "@babel/plugin-transform-modules-commonjs": "^7.12.13", 37 | "@babel/preset-typescript": "^7.12.13", 38 | "@types/jest": "^26.0.20", 39 | "@typescript-eslint/eslint-plugin": "^4.14.2", 40 | "@typescript-eslint/parser": "^4.14.2", 41 | "babel-eslint": "^4.1.6", 42 | "concurrently": "^5.3.0", 43 | "eslint": "^7.19.0", 44 | "eslint-plugin-prettier": "^3.3.1", 45 | "jest": "^26.6.3", 46 | "prettier": "^2.2.1", 47 | "typescript": "^4.1.3" 48 | }, 49 | "directories": { 50 | "test": "test" 51 | }, 52 | "repository": { 53 | "type": "git", 54 | "url": "git+https://github.com/tuxsudo/is-in-browser.git" 55 | }, 56 | "author": "Jared Anderson", 57 | "bugs": { 58 | "url": "https://github.com/tuxsudo/is-in-browser/issues" 59 | }, 60 | "homepage": "https://github.com/tuxsudo/is-in-browser#readme" 61 | } 62 | --------------------------------------------------------------------------------