Bare Utils
`bare-utils` is a JavaScript utility library designed to provide Node.js-compatible functions primarily for the Bare runtime, a specialized environment often used within the Holepunch and Hypercore ecosystems. It offers a subset of commonly used functionalities akin to Node.js's built-in `util` module, ensuring that developers can leverage familiar utility patterns when building applications for Bare. The library's current stable version is 1.6.0. Its release cadence is typically aligned with the development cycle of the Bare runtime itself, focusing on maintaining API parity and addressing specific needs of the ecosystem. A key differentiator is its explicit compatibility and optimization for the Bare environment, making it a targeted tool rather than a general-purpose utility library for standard Node.js projects.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require()` in an ECMAScript module (ESM) file.fixIf in an ESM file, change `const util = require('bare-utils')` to `import util from 'bare-utils'`. Ensure your `package.json` has `"type": "module"` for ESM files or use the `.mjs` extension. -
TypeError: __WEBPACK_IMPORTED_MODULE_0__.format is not a function
cause A bundler (like Webpack) or an ESM environment is attempting to destructure a named export (`format`) from a CommonJS module that exports a default object.fixAdjust the import statement in your ESM or bundled code to `import util from 'bare-utils'`, then access the function as `util.format(...)`.
Warnings
- gotcha This package is primarily a CommonJS module. When used in an ES Module (ESM) environment, developers must use `import util from 'bare-utils'` for CJS interop. Direct named imports such as `import { format } from 'bare-utils'` will not work.
- gotcha The API provided by `bare-utils` is a subset of Node.js's native `util` module. While it aims for compatibility, some functions or specific behaviors present in the full Node.js `util` module might be missing or subtly different. Always verify functionality if migrating from a standard Node.js environment.
Install
-
npm install bare-utils -
yarn add bare-utils -
pnpm add bare-utils
Imports
- util
const util = require('bare-utils') - util
import { format } from 'bare-utils'import util from 'bare-utils'
Quickstart
const util = require('bare-utils');
console.log('Using bare-utils.format to construct strings:');
const name = 'Alice';
const age = 30;
const greeting = util.format('Hello, my name is %s and I am %d years old.', name, age);
console.log(`Example 1: ${greeting}`);
const data = { id: 101, status: 'active' };
const dataString = util.format('User data: %j', data);
console.log(`Example 2: ${dataString}`);
const pi = 3.14159;
const percentage = util.format('Pi to two decimal places: %.2f', pi);
console.log(`Example 3: ${percentage}`);
console.log('\nThis quickstart demonstrates various uses of the `format` function, which behaves similarly to Node.js's `util.format`.');