Typescript lodash no default export for one function

I am trying to import just one function from lodash like this:

import get from 'lodash/get';

      

I have already installed lodash and @ types / lodash and I am getting this error:

@ types / lodash / get / index "'has no default export.)

+7


source to share


5 answers


I managed to get this to work with a similar error using:



import * as get from 'lodash.get';

      

+2


source


You can import one function from lodash using import isEqual from 'lodash/isEqual';

in typescript with flag esModuleInterop

in compiler options (tsconfig.json)

example



{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "lib": ["es6", "dom"],
    "moduleResolution": "node",
    "esModuleInterop": true,
    ...
  }
}

      

+2


source


The only way I could solve it was like this:

import get = require("lodash.get");

See a similar example here: https://github.com/DefiniteTyped/DefiniteTyped/issues/14160

Another suggestion, import * as get from 'lodash.get';

it crashes like this if you don't have the correct configuration: TS2497: This module can only be referenced when importing / exporting ECMAScript by enabling the 'esModuleInterop' flag and providing a link to its default export

And in fact, I have this in my tsconfig.json:

"allowSyntheticDefaultImports": true,
"esModuleInterop": true

      

And it fails anyway (not for React packages, moment and others, but only for this lodash.get package). So the ugly require

does the trick (I mean the ugly just because it breaks ES6 import syntax, which means we're skipping a sequence of code).

0


source


You can use:

import { get } from "lodash";

      

Or:

import { get } from "lodash-es";

      

For the latter, you will need to install the appropriate set file (which apparently enables export by default

):

npm install @types/lodash-es --save-dev

      

-1


source


It looks like there is no default export as the error says. You must do this.

import {get} from 'lodash/get';

      

-2


source







All Articles