Empty Object/Function Utilities
The `empty` utility library, currently at version 0.10.1 (last published over 7 years ago), provides a collection of pre-defined empty objects, arrays, and no-operation functions. Its primary feature is enforcing immutability by applying `Object.freeze` to all returned entities by default. This behavior can be overridden if the `NODE_ENV` environment variable is set to `'production'`. The library was designed primarily for CommonJS environments, explicitly supporting Node.js and older browser bundling workflows via tools like Browserify. Due to its age and lack of recent updates, it does not support modern JavaScript module systems (ESM) or TypeScript natively, requiring manual declarations for type safety. Its key differentiator remains providing a consistent, immutable set of primitive empty values and noop functions.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require()` in a JavaScript file that is treated as an ES module (e.g., `"type": "module"` in `package.json` or `.mjs` extension).fixFor ES Modules, you cannot directly use `require()`. You might need to use dynamic `import('empty')` or `import empty from 'empty';` if your environment supports CJS interop, or convert your module to CJS. The most reliable fix for this package is to ensure the consuming file is CJS or use a bundler. -
TypeError: Cannot assign to read only property 'foo' of object '#<Object>'
cause Attempting to modify an object or array returned by `empty` when `Object.freeze` was applied. This happens when `process.env.NODE_ENV` is *not* `'production'` (the default frozen state).fixDo not attempt to mutate objects or arrays obtained from `empty` as they are immutable by design. If you need a mutable copy, create one using spread syntax (e.g., `{...empty.object}` or `[...empty.array]`). If you explicitly intend for them to be mutable, ensure `process.env.NODE_ENV` is set to `'production'` when the library is loaded.
Warnings
- breaking The library is exclusively CommonJS (CJS). Attempting to use `import` syntax in an ECMAScript Module (ESM) environment without a build step (like webpack or Rollup) or Node.js's CJS compatibility will result in errors.
- gotcha All objects and functions returned by `empty` are `Object.freeze`d by default, making them immutable. However, if `process.env.NODE_ENV` is set to `'production'`, they are *not* frozen. This can lead to unexpected mutability if `NODE_ENV` is changed dynamically or is not consistently managed.
- gotcha This package is effectively abandoned, with the last publish over 7 years ago. It does not provide native TypeScript declarations, meaning consumers will either need to create their own `.d.ts` files or rely on `@ts-ignore`.
Install
-
npm install empty -
yarn add empty -
pnpm add empty
Imports
- empty
import empty from 'empty';
const empty = require('empty'); - empty.object
import { object } from 'empty';const { object } = require('empty'); // or const emptyObject = require('empty/object'); - empty.func
import { func } from 'empty';const { func } = require('empty'); // or const emptyFunction = require('empty/function');
Quickstart
const empty = require('empty');
// Get a frozen empty object
const myEmptyObject = empty.object;
console.log('Empty Object:', myEmptyObject, Object.isFrozen(myEmptyObject));
// Get a frozen empty array
const myEmptyArray = empty.array;
console.log('Empty Array:', myEmptyArray, Object.isFrozen(myEmptyArray));
// Get a no-operation function
const noop = empty.func;
console.log('No-op Function:', noop());
// Get a function that returns a specific value
const returnFoo = empty.functionThatReturns('foo');
console.log('Function that returns "foo":', returnFoo());
// Demonstrating the NODE_ENV impact (typically set via environment variables)
// For demonstration, simulating production behavior where objects might not be frozen
const originalNodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
const unfrozenEmptyObject = require('empty/object'); // Requires re-requiring the module
console.log('Unfrozen Object (if NODE_ENV=production):', unfrozenEmptyObject, Object.isFrozen(unfrozenEmptyObject));
process.env.NODE_ENV = originalNodeEnv; // Restore original env