Tea Extend
tea-extend is a minimalist JavaScript utility designed for performing shallow object merges, allowing properties from one or more source objects to be copied onto a destination object. This package was initially released around 2012, with its current and seemingly final version being 0.2.0. It predates the widespread adoption of native language features such as `Object.assign()` (ES2015) and object spread syntax (ES2018), which now provide equivalent and often more robust functionality directly within JavaScript. Consequently, tea-extend has likely seen no significant updates or active development for over a decade. Its primary differentiator at the time of its creation was providing a simple, early solution for object merging in both Node.js and browser environments (via Component.js) before these native alternatives became standard practice.
Common errors
-
TypeError: (0 , tea_extend_1.default) is not a function
cause This error typically occurs when attempting to use `import extend from 'tea-extend';` or `import { extend } from 'tea-extend';` in an ESM module context for a package that only provides a CommonJS default export.fixFor CommonJS-only modules in an ESM project, access the default export via `import * as teaExtend from 'tea-extend'; const extend = teaExtend.default;`. Alternatively, ensure your file is a CommonJS module, or preferably, replace `tea-extend` with native `Object.assign()` or spread syntax. -
ReferenceError: require is not defined
cause This error indicates that you are attempting to use the CommonJS `require()` function in a file that is being interpreted as an ECMAScript Module (ESM), for example, a `.mjs` file or a file in a project with `"type": "module"` in `package.json`.fixEither convert your file to use ESM `import` syntax (see previous problem entry for how to import CJS modules in ESM) or ensure the file is treated as a CommonJS module. Given the package's age, the best fix is to replace `tea-extend` entirely with `Object.assign()` or object spread syntax.
Warnings
- gotcha tea-extend performs only shallow merges. Nested objects within source objects are copied by reference, not recursively cloned. This means modifications to nested objects in the destination will directly affect the original source objects if they shared references.
- breaking This package is a CommonJS module (`module.exports`). Attempting to use direct ESM `import` syntax (e.g., `import extend from 'tea-extend';` or `import { extend } from 'tea-extend';`) in an ECMAScript Module (ESM) context may lead to runtime errors or incorrect behavior due to how Node.js handles CJS interop with ESM.
- deprecated The functionality provided by `tea-extend` is now natively available and more robustly implemented through `Object.assign()` (ES2015) and the object spread syntax `{ ...obj1, ...obj2 }` (ES2018). Relying on this older, unmaintained utility is generally discouraged for modern JavaScript development.
Install
-
npm install tea-extend -
yarn add tea-extend -
pnpm add tea-extend
Imports
- extend
import extend from 'tea-extend';
const extend = require('tea-extend'); - extend function (ESM interop)
import { extend } from 'tea-extend';import * as teaExtend from 'tea-extend'; const extend = teaExtend.default;
Quickstart
const extend = require('tea-extend');
// Sample objects
const a = { hello: 'world', data: { id: 1 } };
const b = { speak: 'loudly', data: { value: 'test' } };
const c = { foo: 'bar' };
// Extend 'a' with properties from 'b'
extend(a, b);
console.log('Object a after extend(a, b):', a);
// Expected: { hello: 'world', data: { value: 'test' }, speak: 'loudly' }
// Shallow clone to 'c' from 'a'
const clonedC = extend({}, a, c);
console.log('Cloned object c:', clonedC);
// Expected: { hello: 'world', data: { value: 'test' }, speak: 'loudly', foo: 'bar' }
// Demonstrate shallow copy behavior: modifying nested 'data' in 'a' affects 'b' if not deeply copied
a.data.value = 'modified';
console.log('Object b after modifying a.data:', b); // b.data.value will be 'modified'