Registry / serialization / extend-shallow

extend-shallow

JSON →
library3.0.2jsnpmunverified

extend-shallow is a minimalist JavaScript utility designed to merge the enumerable properties of one or more source objects into a target object. It performs a *shallow* merge, meaning only top-level properties are copied; nested objects or arrays are copied by reference rather than being cloned. The current stable version is 3.0.2, last published in 2017, indicating a mature and stable, but not actively feature-developed, codebase. It primarily targets Node.js environments from version 0.10.0 upwards, supporting CommonJS module syntax. While modern JavaScript environments widely support `Object.assign()` for similar functionality, extend-shallow serves as a lightweight alternative, particularly useful in legacy environments or when a direct, unopinionated shallow merge is explicitly required without the overhead of polyfills or broader compatibility layers. Its key differentiator lies in its focused, single-purpose design, making it a very small footprint utility.

npm install extend-shallow
INSTALL
IMPORT
SIG · EXTEND-SHALLOW
E
extend-shallow
serializationjavascriptv3.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

extend
const extend = require('extend-shallow');
import { extend } from 'extend-shallow';
This package is a CommonJS module. Named imports are not supported and will fail. Use `require` or a default ESM import via Node's interoperability.
extend (ESM)
import extend from 'extend-shallow';
import * as extend from 'extend-shallow';
While primarily CommonJS, Node.js's interoperability allows for default ESM imports. Avoid wildcard imports unless you explicitly need the entire module object.

Illustrates how to perform a basic shallow extension with multiple source objects and how to create a shallow clone, highlighting the reference-copy behavior for nested objects.

const extend = require('extend-shallow'); // Basic shallow extension const targetObj = { a: 1, b: { c: 2 } }; const sourceObj1 = { b: { d: 3 }, e: 4 }; const sourceObj2 = { f: 5 }; const result = extend(targetObj, sourceObj1, sourceObj2); console.log('Extended object:', result); // Expected output: { a: 1, b: { d: 3 }, e: 4, f: 5 } // Note: b from sourceObj1 overwrites b from targetObj. Nested {d:3} is copied by reference. // Shallow clone by extending into an empty object const original = { x: 10, y: { z: 20 } }; const clone = extend({}, original); console.log('Shallow clone:', clone); console.log('Is original.y === clone.y?', original.y === clone.y); // true, because it's a shallow copy // Demonstrating the shallow copy behavior clone.y.z = 99; console.log('Original after modifying clone.y:', original); // Expected: Original will also show { x: 10, y: { z: 99 } } because y is a shared reference.
Debug
Known issues
gotchaextend-shallow performs a shallow merge. This means that only top-level properties are copied by value. Nested objects or arrays within source objects are copied by reference to the target object. Modifying a nested property in the target object will therefore affect the original source object if they share the same reference.
fix
For deep merging, consider using libraries specifically designed for deep cloning or merging, such as 'lodash.merge', 'deepmerge', or manually implementing a recursive merge function for controlled behavior.
affects: >=0.1.0
gotchaThis package is a CommonJS module and does not natively support ES Modules (ESM) named imports. Attempting `import { extend } from 'extend-shallow'` will result in a runtime error or unexpected `undefined` value.
fix
Always use CommonJS `const extend = require('extend-shallow');` or a default ESM import `import extend from 'extend-shallow';` which Node.js handles via interoperability rules.
affects: >=0.1.0
gotchaWhile still functional and stable, extend-shallow is largely superseded by the native JavaScript method `Object.assign()`. `Object.assign()` provides similar shallow merging capabilities and is widely supported in modern JavaScript environments (ES6+).
fix
For new projects or modern environments, prefer `Object.assign()` unless you have a specific requirement for `extend-shallow`'s exact implementation details or need to support extremely legacy JavaScript environments (pre-ES6) without polyfills.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: extend_shallow_1.extend is not a function
This error typically occurs when using TypeScript or ESM and attempting a named import (e.g., `import { extend } from 'extend-shallow';`) from a CommonJS module that only provides a default export.
fix
Use a default import for ESM (`import extend from 'extend-shallow';`) or the CommonJS `require` syntax (`const extend = require('extend-shallow');`).
TypeError: Cannot set property 'someProp' of undefined (or null)
The first argument to `extend-shallow` (the target object) must be a non-null, non-undefined object. If `null`, `undefined`, or a primitive value is passed as the target, attempts to assign properties will fail.
fix
Ensure the first argument passed to `extend` is always an object, typically an empty object literal `{}` if you intend to create a new object or an existing object you wish to mutate.
Upgrade
Version history
3.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources