Bare OS Utilities
bare-os provides essential operating system utilities tailored for the `bare` JavaScript runtime, a lightweight and modular environment designed for peer-to-peer applications across desktop and mobile devices. Unlike Node.js, the `bare` runtime emphasizes a minimal core, relying on modular userland libraries like `bare-os` for functionalities typically found in a standard library. Currently at version 3.8.7, the package exhibits active development, with recent updates and a healthy maintenance status. It is a key component within the Holepunch ecosystem, focusing on efficiency and cross-platform compatibility, particularly for networked applications requiring direct user-to-user connections without traditional servers. The library ships with TypeScript types, ensuring robust development experiences.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'platform')
cause Attempting to access a property on an undefined `os` object, likely due to incorrect ESM default import or CJS non-destructured `require`.fixCorrect your import/require statement: `import { platform } from 'bare-os';` or `const { platform } = require('bare-os');` -
Error: Module not found: Can't resolve 'bare-os'
cause The package `bare-os` is either not installed or the runtime environment does not recognize it.fixInstall the package via `npm i bare-os` and ensure you are running your application in the `bare` runtime environment.
Warnings
- breaking `bare-os` is specifically designed for the `bare` JavaScript runtime and is not directly compatible with Node.js or browser environments.
- gotcha The `bare` runtime's compile options, which can influence underlying behaviors, are not covered by semantic versioning and may change without warning, potentially affecting `bare-os` behavior.
- gotcha The `bare-os` module exports named functions directly. Attempting to import it as a default export or access properties on a `require`'d object without destructuring can lead to undefined errors.
Install
-
npm install bare-os -
yarn add bare-os -
pnpm add bare-os
Imports
- platform
const os = require('bare-os'); os.platform();import { platform } from 'bare-os'; - homedir
import os from 'bare-os'; os.homedir();
import { homedir, tmpdir } from 'bare-os'; - totalmem
const totalmem = require('bare-os').totalmem;const { totalmem } = require('bare-os');
Quickstart
import { platform, arch, homedir, totalmem, freemem, uptime } from 'bare-os';
async function getSystemInfo() {
console.log(`
--- System Information ---
`);
console.log(`Platform: ${platform()}`);
console.log(`Architecture: ${arch()}`);
console.log(`Home Directory: ${homedir()}`);
const totalMemoryBytes = totalmem();
const freeMemoryBytes = freemem();
console.log(`Total Memory: ${(totalMemoryBytes / (1024 ** 3)).toFixed(2)} GB`);
console.log(`Free Memory: ${(freeMemoryBytes / (1024 ** 3)).toFixed(2)} GB`);
const systemUptimeSeconds = uptime();
const hours = Math.floor(systemUptimeSeconds / 3600);
const minutes = Math.floor((systemUptimeSeconds % 3600) / 60);
console.log(`System Uptime: ${hours}h ${minutes}m`);
console.log(`
--------------------------
`);
}
getSystemInfo().catch(console.error);