https://github.com/dandre3000/environment
https://github.com/dandre3000/environment
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/dandre3000/environment
- Owner: dandre3000
- License: mit
- Created: 2026-04-16T19:45:58.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2026-04-17T00:35:30.000Z (2 months ago)
- Last Synced: 2026-04-20T02:44:21.780Z (2 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @dandre3000/environment
Check if code is running on the main thread or a worker of a browser, node, bun or deno instance.
## Installation
npm i @dandre3000/environment
## Usage
```js
import { isMainThread, runtime } from '@dandre3000/environment'
if (isMainThread) {
const url = new URL(import.meta.url)
if (runtime === 'node') {
const worker = new (await import('node:worker_threads')).Worker(url)
worker.on('message', data => console.log(data))
} else {
const worker = new Worker(url, { type: 'module' })
worker.addEventListener('message', ({ data }) => console.log(data))
}
} else {
if (runtime === 'node') {
(await import('node:worker_threads')).parentPort.postMessage('YOOO')
} else {
postMessage('YOOO')
}
}
```
## Exports
#### runtime: 'bun' | 'browser' | 'deno' | 'node'
The name of the current runtme.
#### isMainThread: boolean
True if the current global scope is the main thread or false otherwise.
## License
[MIT](https://github.com/dandre3000/environment/blob/main/LICENSE)