{"id":10865,"library":"extend-shallow","title":"Extend Shallow","description":"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.","status":"maintenance","version":"3.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/jonschlinkert/extend-shallow","tags":["javascript","assign","clone","extend","merge","obj","object","object-assign","object.assign"],"install":[{"cmd":"npm install extend-shallow","lang":"bash","label":"npm"},{"cmd":"yarn add extend-shallow","lang":"bash","label":"yarn"},{"cmd":"pnpm add extend-shallow","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import { extend } from 'extend-shallow';","symbol":"extend","correct":"const extend = require('extend-shallow');"},{"note":"While primarily CommonJS, Node.js's interoperability allows for default ESM imports. Avoid wildcard imports unless you explicitly need the entire module object.","wrong":"import * as extend from 'extend-shallow';","symbol":"extend (ESM)","correct":"import extend from 'extend-shallow';"}],"quickstart":{"code":"const extend = require('extend-shallow');\n\n// Basic shallow extension\nconst targetObj = { a: 1, b: { c: 2 } };\nconst sourceObj1 = { b: { d: 3 }, e: 4 };\nconst sourceObj2 = { f: 5 };\n\nconst result = extend(targetObj, sourceObj1, sourceObj2);\nconsole.log('Extended object:', result);\n// Expected output: { a: 1, b: { d: 3 }, e: 4, f: 5 }\n// Note: b from sourceObj1 overwrites b from targetObj. Nested {d:3} is copied by reference.\n\n// Shallow clone by extending into an empty object\nconst original = { x: 10, y: { z: 20 } };\nconst clone = extend({}, original);\nconsole.log('Shallow clone:', clone);\nconsole.log('Is original.y === clone.y?', original.y === clone.y); // true, because it's a shallow copy\n\n// Demonstrating the shallow copy behavior\nclone.y.z = 99;\nconsole.log('Original after modifying clone.y:', original);\n// Expected: Original will also show { x: 10, y: { z: 99 } } because y is a shared reference.","lang":"javascript","description":"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."},"warnings":[{"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.","message":"extend-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.","severity":"gotcha","affected_versions":">=0.1.0"},{"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.","message":"This 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.","severity":"gotcha","affected_versions":">=0.1.0"},{"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.","message":"While 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+).","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use a default import for ESM (`import extend from 'extend-shallow';`) or the CommonJS `require` syntax (`const extend = require('extend-shallow');`).","cause":"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.","error":"TypeError: extend_shallow_1.extend is not a function"},{"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.","cause":"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.","error":"TypeError: Cannot set property 'someProp' of undefined (or null)"}],"ecosystem":"npm"}