Build Utilities Collection
build-utils is a JavaScript library offering a collection of utility functions primarily designed for build scripts. It encapsulates common operations across file system manipulation (`fs`), process execution (`process`), configuration updates (`config`), object transformations (`object`), and command-line interface (`cli`) helpers. The current stable version is 2.0.12, but it was last published on npm approximately eight years ago (August 2018). Due to its age, it primarily uses the CommonJS module system and is not actively maintained, meaning there are no new features, bug fixes, or security updates. It serves as a straightforward, albeit outdated, toolkit for basic automation tasks, lacking the modern features or dual ESM/CJS support found in contemporary alternatives.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require()` in an ECMAScript Module (ESM) context when `build-utils` is a CommonJS-only package.fixChange your file's extension to `.cjs` or ensure your `package.json` specifies `"type": "commonjs"`. Alternatively, use dynamic `import()` for CJS modules within ESM, but be aware of potential interoperability complexities. -
TypeError: Cannot read properties of undefined (reading 'createDirectory')
cause This error typically occurs when trying to destructure a property from `undefined`. It implies that `require('build-utils')` did not return an object with a `fs` property, or `fs` did not have `createDirectory`, likely due to incorrect import path or module resolution issues.fixEnsure you are correctly accessing utilities under their respective namespaces (e.g., `require('build-utils').fs.createDirectory`). Verify the exact function name and nesting from the package's source or documentation. -
Error: spawn ENOENT
cause The `exec` or `spawn` utility within `build-utils.process` could not find the specified command in the system's PATH. This is a common operating system error when trying to run an unrecognized executable.fixCheck that the command you are trying to execute (`ls`, `git`, `npm`, etc.) is installed and accessible in the system's PATH environment variable. Provide the full path to the executable if it's not globally available.
Warnings
- breaking The `build-utils` package was last published in August 2018. It is unmaintained, which means there will be no new features, bug fixes, or security updates. Users should consider migrating to actively maintained alternatives for critical projects.
- breaking This package is exclusively designed for CommonJS (CJS) environments and does not support ECMAScript Modules (ESM) syntax. Attempting to use `import` statements will lead to runtime errors in Node.js ESM projects.
- gotcha Compatibility with newer Node.js versions is uncertain due to the package's age. It may rely on deprecated Node.js APIs or exhibit unexpected behavior with recent runtime changes, potentially leading to instability or subtle bugs.
- gotcha The globbing utilities (`copyGlob`, `deleteGlob`, `searchGlob`) might use an outdated or custom glob implementation. This could lead to different behavior compared to modern, widely adopted globbing libraries (e.g., `fast-glob`), affecting pattern matching and file resolution.
Install
-
npm install build-utils -
yarn add build-utils -
pnpm add build-utils
Imports
- createDirectory
import { createDirectory } from 'build-utils';const { createDirectory } = require('build-utils'); - spawn
import { process } from 'build-utils'; const { spawn } = process;const { spawn } = require('build-utils').process; - deepAssign
import deepAssign from 'build-utils/object';
const { deepAssign } = require('build-utils').object;
Quickstart
const { createDirectory, writeFile, deleteDirectory } = require('build-utils').fs;
const { exec } = require('build-utils').process;
const path = require('path');
async function runBuildProcess() {
const tempDir = path.join(__dirname, 'temp-output');
const tempFile = path.join(tempDir, 'artifact.txt');
console.log(`Creating directory: ${tempDir}`);
await createDirectory(tempDir);
console.log('Directory created.');
console.log(`Writing file: ${tempFile}`);
await writeFile(tempFile, 'This is a build artifact generated by build-utils.\nHello, World!');
console.log('File written.');
console.log('Executing a command: ls');
try {
const { stdout, stderr } = await exec('ls -l ' + tempDir);
console.log('Stdout:', stdout);
if (stderr) console.error('Stderr:', stderr);
} catch (error) {
console.error('Error executing command:', error.message);
}
console.log(`Cleaning up directory: ${tempDir}`);
await deleteDirectory(tempDir);
console.log('Cleanup complete.');
}
runBuildProcess().catch(console.error);