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.)
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';
我设法通过使用以下方法来解决类似的错误:
import * as get from 'lodash.get';
Only way I could solve this was like:
import get = require("lodash.get");
See a similar example here:https://github.com/DefinitelyTyped/DefinitelyTyped/issues/14160
The other suggestion, import * as get from 'lodash.get';
fails like this if you don't have the proper configuration: TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export
And actually, I have this in my tsconfig.json:
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
And it fails anyway (not for React , moment and other packages, but only for this lodash.get package). So, the ugly require
do the trick (I mean ugly just because it breaks the ES6 import syntax, meaning we miss the code consistency).
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';
您需要执行以下操作:
"allowSyntheticDefaultImports": true
It looks like there's no default export as the error says. You should get it this way.
import {get} from 'lodash/get';
You can either use:
import { get } from "lodash";
Or:
import { get } from "lodash-es";
For the latter you will need to install the appropriate typing file (which apparently includes the default
export):
npm install @types/lodash-es --save-dev
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.