Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript lodash no default export for one function

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

import get from 'lodash/get';

I already installed lodash and @types/lodash, and I'm getting this error:

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

like image 442
ng2user Avatar asked Mar 27 '17 08:03

ng2user


People also ask

How to import a function from Lodash into typescript?

Bookmark this question. Show activity on this post. @types/lodash/get/index"' has no default export.) Show activity on this post. You can import a single function from lodash using import isEqual from 'lodash/isEqual'; in typescript with the esModuleInterop flag in the compiler options (tsconfig.json)

How do you import a function in typescript?

TypeScript uses the concept of modules , in the same way that JavaScript does. In order to be able to import a function from a different file, it has to be exported using a named or default export. The example above uses a named export and a named import.

When should I use export default in typescript?

TypeScript recommends default export for modules with a primary purpose [4]: If you’re only exporting a single class or function, use export default If a module’s primary purpose is to house one specific export, then you should consider exporting it as a default export. This makes both importing and actually using the import a little easier.

How to solve the'module has no default export'error?

The "Module has no default export" error occurs when we try to import as default from a module that doesn't have a default export. To solve the error make sure the module has a named export and wrap the import in curly braces, e.g. import {myFunction} from './myModule'. Here is an example of how the error occurs. This is our index.ts file. Copied!


3 Answers

You can import a single function from lodash using import isEqual from 'lodash/isEqual'; in typescript with the esModuleInterop flag in the compiler options (tsconfig.json)

example

{   "compilerOptions": {     "module": "commonjs",     "target": "es5",     "lib": ["es6", "dom"],     "moduleResolution": "node",     "esModuleInterop": true,     ...   } } 
like image 100
Alex Beauchemin Avatar answered Oct 10 '22 01:10

Alex Beauchemin


i had same problem, and i ended up here,

so here is the solution:

you need to install lodash-es

npm install --save lodash-es 

instead of normal lodash

and read this link

https://medium.com/@armno/til-importing-lodash-into-angular-the-better-way-aacbeaa40473

update 1: it should really be easier, but anyway

point 1: install typings

 npm i -D @types/lodash-es 

point 2: import from "lodash-es" not "lodash"

point 3: import like this, it's proper way (otherwise you will get a much larger bundle)

import  orderBy  from 'lodash-es/orderBy'; 
like image 25
Amirreza Avatar answered Oct 10 '22 01:10

Amirreza


This is what I had to do to make it working for lodash.random, which I think should reasonably work for lodash.get too :

  • Install the function of lodash you want with npm install --save lodash.random
  • Install the types linked to it: npm install --save-dev @types/lodash.random
  • Set compilerOptions.allowSyntheticDefaultImports to true in your tsconfig.json
  • Import it like import { random } from 'lodash';
like image 32
Hadrien TOMA Avatar answered Oct 10 '22 03:10

Hadrien TOMA