lodash-es is the ES6 module version of the Lodash utility library, designed specifically for modern JavaScript applications that support tree-shaking to reduce bundle sizes. Unlike the standard lodash package which uses CommonJS and often imports the entire library even for single functions, lodash-es allows developers to import only the specific utilities they need, such as debounce, cloneDeep, or map, resulting in smaller and more efficient builds.
Key characteristics and usage include:
Modular Architecture: Functions are available as separate ES6 modules (e.g.,
import map from "lodash-es/map.js") or via named imports from the main package (e.g.,import { map, filter } from "lodash-es").Tree-Shaking Support: Modern bundlers like Webpack and Rollup can eliminate unused code, ensuring that only the functions actually used in the project are included in the final bundle.
TypeScript Compatibility: It provides excellent out-of-the-box TypeScript support with built-in type definitions, offering better autocompletion and error catching compared to the standard library.
Functionality: It offers the same 300+ utility functions as standard Lodash, covering arrays, collections, objects, strings, functions, and more, organized into logical categories.
| Feature | lodash | lodash-es |
| Module System | CommonJS | ES6 Modules (ESM) |
| Tree-Shaking | Not supported | Fully supported |
| Bundle Size | Larger (imports all) | Smaller (imports only used) |
| Environment | All JS environments (including older) | Modern ES6+ environments |
| Import Syntax | require('lodash') | import { func } from 'lodash-es' |
While lodash-es is ideal for modern projects, an alternative library called es-toolkit is gaining traction as a faster and significantly smaller (up to 97% smaller) drop-in replacement that also offers 100% compatibility with Lodash functions.
» npm install lodash-es
"es-toolkit", a 2-3x faster and 97% smaller alternative to lodash
Merge lodash-es into lodash package
es-toolkit, a drop-in replacement for Lodash, achieves 100% compatibility
es-toolkit, a drop-in replacement for Lodash, achieves 100% compatibility
Videos
You can load types from here:
npm install --save-dev @types/lodash-es
And use it like:
import { camelCase } from "lodash-es"
ts-node compiles .ts files as they are loaded. It doesn't touch .js files; either they must already be in a form understood by Node.js (e.g., no ES6 import statements) or you must use a separate mechanism to transform them, such as the @babel/register require hook (essentially the same thing that is used by babel-node). You would still need to configure @babel/register not to ignore node_modules, as described in the other answer. The advice from the other answer to just use lodash instead of lodash-es is good.
» npm install @types/lodash-es