amp-has utility (re-export of lodash.has)
The `amp-has` package provides a simple utility function, `has`, designed to check for the existence of nested properties within an object. It is part of the `ampersand.js` ecosystem, which was a collection of loosely coupled JavaScript modules for client-side applications. The package, currently at version 1.0.1, was last updated in April 2016 and is essentially a direct re-export of the `lodash.has` function. Given the minimal activity on the `ampersand.js` project and this specific utility's unmaintained status for many years, its release cadence is effectively ceased. Its key differentiator was its inclusion in the `ampersand.js` modular approach; however, modern applications are strongly advised to use `lodash.has` directly, which is actively maintained and supports contemporary JavaScript module systems.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use `import { has } from 'amp-has'` in a Node.js project that is configured as a CommonJS module or does not have `"type": "module"` in its `package.json`.fixIf staying with `amp-has`, use `const has = require('amp-has');`. Otherwise, migrate to `lodash.has` which offers better ESM support with modern tooling. -
TypeError: has is not a function
cause This error can occur if you attempt to destructure `require('amp-has')` (e.g., `const { has } = require('amp-has')`) or use an incorrect `import` statement in a CJS context, as `amp-has` exports the `has` function directly as its `module.exports`.fixThe correct CommonJS import is `const has = require('amp-has');`.
Warnings
- deprecated `amp-has` is unmaintained since 2016 and is merely a re-export of `lodash.has`. It is strongly recommended to switch directly to `lodash.has` to ensure you are using a current, maintained dependency and avoid potential compatibility issues.
- gotcha This package is a CommonJS (CJS) module and does not natively support ES Module (ESM) `import` syntax. Attempting to use `import` statements will lead to runtime errors in pure ESM environments.
Install
-
npm install amp-has -
yarn add amp-has -
pnpm add amp-has
Imports
- has
import { has } from 'amp-has'; import has from 'amp-has';const has = require('amp-has');
Quickstart
const has = require('amp-has');
const user = {
id: 1,
name: 'Alice',
address: {
street: '123 Main St',
city: 'Anytown',
zip: '12345'
},
contact: null
};
console.log('User has address.city:', has(user, 'address.city'));
// Expected: true
console.log('User has address.country:', has(user, 'address.country'));
// Expected: false
console.log('User has id:', has(user, 'id'));
// Expected: true
console.log('User has contact.email:', has(user, 'contact.email'));
// Expected: false (handles null paths gracefully)
// For modern usage, it's recommended to use lodash.has directly:
// const lodashHas = require('lodash.has');
// console.log('Using lodash.has directly:', lodashHas(user, 'address.zip'));