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.)
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)
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.
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.
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!
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, ... } }
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';
This is what I had to do to make it working for lodash.random
, which I think should reasonably work for lodash.get
too :
npm install --save lodash.random
npm install --save-dev @types/lodash.random
compilerOptions.allowSyntheticDefaultImports
to true
in your tsconfig.json
import { random } from 'lodash';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With