@dekkai/env
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@dekkai/env
browser node deno opinion

@dekkai/env

Utility methods to detect runtimes and load modules dynamically.

Check out the full API Documentation

Installation

Browser/NodeJS

$ yarn add @dekkai/env

Deno

// import from directly from a CDN, like unpkg.com
import {isDeno, isBrowser, isNodeJS, loadModule} from 'https://unpkg.com/@dekkai/env';

Usage

// import the desired methods
import {isDeno, isBrowser, isNodeJS, loadModule} from '@dekkai/env';

// test for deno
const deno = isDeno();

// test for node
const node = isNodeJS();

// test for browser
const browser = isBrowser();

// import a module at runtime, this is generally the same as calling `await import(module)` but it is safe for all 
// platforms and should be able to survive bundlers, also wraps `require` for node environments where needed.
// The main advantage is to import modules needed for a specific platform at runtime, think importing `fs` for node
// at runtime but still being able to compile for all platforms.
let fs;
if (node) {
    fs = await loadModule('fs');
} else if (deno) {
    fs = await loadModule('https://deno.land/std/fs/mod.ts');
} else {
    fs = await loadModule('https://unpkg.com/browserfs');
}
// ...

Check out the full API Documentation

Package Sidebar

Install

npm i @dekkai/env

Weekly Downloads

502

Version

1.1.0

License

MIT

Unpacked Size

23.5 kB

Total Files

30

Last publish

Collaborators

  • darionco