2

I'm trying to use lodash (v4.17.11) features with cherry picking import in my project. When I do this:

import {chain} from 'lodash';

and

chain([1,2,3]).take(1)

it works fine, however, if I change the import to:

import chain from 'lodash/chain';

the output is:

TypeError: (0 , _chain2.default)(...).take is not a function

Can someone please explain what's the mistake here

2 Answers2

1

import chain from lodash/chain only works if there is a default export from the module.

If you want to import a particular named export - import {chain} from lodash/chain is the right way

See this - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

Mortz
  • 4,654
  • 1
  • 19
  • 35
0

When you are using:

import {chain} from 'lodash/chain';

This treats the chain as named export not the default export from lodash/chain. But whenever you are point to a module lodash, in your case chain, module would be a default export rather than a named export. That is why when you use import chain from 'lodash/chain';, it brings the default export from the module.

chain would be a named export is you are importing it from the root of lodash as following:

import {chain} from 'lodash';

You should probably have a look at export and import in javascript and have a better understanding of it.

Pranay Tripathi
  • 1,614
  • 1
  • 16
  • 24