lodash.first
raw JSON → 3.0.0 verified Sat Apr 25 auth: no javascript maintenance
The lodash.first package provides the modern build of lodash's `_.first` function as a standalone npm module, version 3.0.0. This package is part of lodash's modularized approach, allowing developers to import only the specific utility they need, reducing bundle size compared to using the full lodash library. Note that since lodash v4, `_.first` has been deprecated in favor of `_.head`, which offers identical functionality. The modularized packages like lodash.first are frozen at lodash v3.0.0 and are not updated with new releases; they remain in maintenance mode.
Common errors
error Cannot find module 'lodash.first' ↓
cause Package not installed or missing from node_modules.
fix
Run 'npm install lodash.first' in your project directory.
error TypeError: first is not a function ↓
cause Incorrect import style (e.g., default import) or missing require.
fix
Use 'var first = require('lodash.first');' and ensure package is installed.
Warnings
gotcha lodash.first is frozen at v3.0.0 and will not receive updates. Use lodash.head instead for current lodash API. ↓
fix Switch to require('lodash.head') or use the full lodash v4+.
deprecated _.first deprecated in lodash v4 in favor of _.head. ↓
fix Use _.head instead.
breaking In lodash v3, _.first expects an array-like object. Passing a non-array-like (e.g., number) may throw or return undefined. ↓
fix Ensure input is array-like before calling first.
Install
npm install lodash.first yarn add lodash.first pnpm add lodash.first Imports
- first wrong
import first from 'lodash.first'correctvar first = require('lodash.first') - _.first wrong
var first = require('lodash/first')correctvar _ = require('lodash'); var first = _.first - head wrong
var first = require('lodash.first')correctvar head = require('lodash.head')
Quickstart
var first = require('lodash.first');
// Get first element of an array
console.log(first([1, 2, 3])); // 1
console.log(first([])); // undefined
// Works with strings too
console.log(first('hello')); // 'h'
// Works on array-like objects
var args = function() { return arguments; };
console.log(first(args('a', 'b'))); // 'a'