Skip to main content

Using webpack, I'm trying to import isEqual since lodash seems to be importing everything. I've tried doing the following with no success.

---
title: How to import a single Lodash function
subtitle: "Using webpack, I'm trying to import isEqual since lodash seems to be importing everything. I've tried doing the following with no success"
author: StackOverflow
date: January 15, 2019
source: https://stackoverflow.com/a/43479515
snippet: https://jonlabelle.com/snippets/view/markdown/how-to-import-a-single-lodash-function
notoc: false
---

You can install `lodash.isequal` as a single module without installing the whole
*lodash* package like so:

```console
npm install --save lodash.isequal
```

When using ECMAScript 5 and CommonJS modules, you then import it like this:

```javascript
var isEqual = require('lodash.isequal');
```

Using ES6 modules, this would be:

```javascript
import isEqual from 'lodash.isequal';
```

And you can use it in your code:

```javascript
const obj1 = {username: 'peter'};
const obj2 = {username: 'peter'};
const obj3 = {username: 'gregory'};

isEqual(obj1, obj2) // returns true
isEqual(obj1, obj3) // returns false
```

Source: [Lodash documentation](https://www.npmjs.com/package/lodash.isequal)

After importing, you can use the isEqual function in your code. Note that it is
not a part of an object named `_` if you import it this way, so you don't
reference it with `_.isEqual`, but directly with `isEqual`.

**Alternative: Using lodash-es**

As pointed out by @kimamula:

With webpack 4 and [lodash-es](https://www.npmjs.com/package/lodash-es) 4.17.7 and higher, this code works.

```javascript
import { isEqual } from 'lodash-es';
```

This is because webpack 4 supports the [sideEffects](https://webpack.js.org/guides/tree-shaking/#mark-the-file-as-side-effect-free)
flag and `lodash-es` 4.17.7 and higher includes the flag (which is set to `false`).

**Why Not Use the Version With the Slash?** Other answers to this question
suggest that you can also use a dash instead of a dot, like so:

```javascript
import isEqual from 'lodash/isequal';
```

This works, too, but there are two minor drawbacks:

- You have to install the whole lodash package (`npm install --save lodash`),
  not just the small separate *lodash.isequal* package; storage space is cheap
  and CPUs are fast, so you may not care about this
- The resulting bundle when using tools like webpack will be slightly bigger; I
  found out that bundle sizes with a minimal code example of `isEqual` are on
  average 28% bigger (tried webpack 2 and webpack 3, with or without Babel, with
  or without Uglify)

See all available [Lodash modules on npmjs.com](https://www.npmjs.com/search?q=keywords:lodash-modularized).