GExtend Object Utility
GExtend is a concise utility module designed for robust object extension, supporting both Node.js and browser environments. The package, currently at version 0.8.0, provides a flexible `extend` function to merge properties from multiple source objects into a target object, analogous to `jQuery.extend` or `Object.assign`. It includes specialized methods like `extend.only` for selective property merging, `extend.shim` to safely wrap objects (e.g., `console`) without direct modification, and `extend.unshim` for reverting shims, making it suitable for creating configurable and insulated class instances. Despite its utility, the project appears to be largely unmaintained, with its last recorded release history updates dating back to 2019. Its primary distribution method and usage patterns predate modern JavaScript module conventions, relying heavily on CommonJS and mentioning Bower for frontend integration.
Common errors
-
Error: Cannot find module 'gextend'
cause The package 'gextend' has not been installed, or the CommonJS `require()` path is incorrect, or you are trying to use an ESM `import` in a context where it's not resolved properly.fixFirst, ensure the package is installed: `npm install gextend`. If using ESM, try `import * as extend from 'gextend';` or verify your build configuration supports CJS module interoperability. -
TypeError: extend is not a function
cause This usually occurs when attempting to call `extend()` directly after an incorrect `import` statement in an ESM context, or if the `require('gextend')` result is not assigned to a variable named `extend`.fixVerify that you are using `const extend = require('gextend');` in CommonJS. If in ESM, ensure you are importing it as an object (e.g., `import * as extend from 'gextend';`) and then calling `extend.default()` or `extend({}, ...)` if it exposes itself as the default. -
EACCES: permission denied, mkdir '/usr/local/lib/node_modules/gextend'
cause This error, often seen on older Node.js setups, indicates insufficient permissions for npm to write to global installation directories, typically `/usr/local/lib/node_modules`.fixAvoid using `sudo npm install -g` as it can lead to permission issues. Instead, fix permissions by running `sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}` or, preferably, use a Node Version Manager (like nvm) to manage Node.js versions and packages without requiring `sudo`.
Warnings
- breaking Starting with v0.4.0, the `extend` function changed its behavior when handling functions during merging. Previously, functions might have been extended, but now they are overwritten directly, which can change object behavior if you were relying on function merging.
- gotcha The `gextend` package appears to be an abandoned project. Its last activity in the release history dates back to 2019, and it references deprecated tools like Bower.js. This implies no active maintenance, security updates, or feature development, making it potentially unsuitable for new projects or critical applications.
- gotcha GExtend is primarily a CommonJS (CJS) module. While it can be used in modern Node.js ESM environments via interoperability layers, direct `import extend from 'gextend';` might not work as expected without specific `type: 'module'` in `package.json` and/or bundler configurations.
- deprecated The README extensively mentions and provides instructions for Bower, a package manager for the web that has largely been superseded by npm and Yarn for frontend dependency management. This further highlights the package's age and lack of modern tooling adoption.
Install
-
npm install gextend -
yarn add gextend -
pnpm add gextend
Imports
- extend
import extend from 'gextend';
const extend = require('gextend'); - extend (ESM compatibility)
import { extend } from 'gextend';import * as extend from 'gextend';
- extend.only, extend.shim, extend.unshim
import { only, shim } from 'gextend';const extend = require('gextend'); // ... later ... extend.only(attributes);
Quickstart
const extend = require('gextend');
const DEFAULTS = {
port: 9090,
url: 'http://localhost'
};
function connect(options){
// Merges options into DEFAULTS, then into a new empty object.
// The empty object {} ensures DEFAULTS is not mutated.
let config = extend({}, DEFAULTS, options);
console.log('Connect to %s:%s', config.url, config.port);
}
// Example usage
connect({
url: 'http://127.0.0.1'
});
connect({
port: 8080
});
connect({});