I'm on Angular 7, I've installed lodash 4.17.11 and @types/lodash 4.14.120, and I tried two way to import it
import _ from 'loadsh';
import * as _ from 'lodash';
but I always got error TS1192: Module '"/myproject/node_modules/@types/lodash/index"' has no default export.
You can import lodash with the following statement:
import _ = require('lodash');
It doesn't work because of an incompatibility between the CommonJS modules (what node uses) and ECMAScript modules. TypeScript differentiates between the two module systems as opposed to Babel which tries to smooth out the differences. You can look up more about the topic here.
You can import
lodashwith the following statement:import _ = require('lodash');It doesn't work because of an incompatibility between the CommonJS modules (what node uses) and ECMAScript modules. TypeScript differentiates between the two module systems as opposed to Babel which tries to smooth out the differences. You can look up more about the topic here.
Thanks, but got another
[ts] Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. [1202]
btw, I'm on vscode 1.30.2
Then this one is your friend. This way you can use
import * as _ from 'lodash';
and it should work.
And you should really read the part of the readme I've linked in my previos answer. And the follow-up links.
Thanks @BendingBender , now it properly worked but I'm still wondering if the @types/lodash can add a default export
The most correct way to import a lodash operator, at least with Angular 8, is to do:
import uniqBy from 'lodash/uniqBy';
*Note... VSCode shows an error in red on that line:
[ts] Module '"/build/brower/gitrepos/FaceVACS-VideoScan-WA-Development/src/web-apps/angular/node_modules/@types/lodash/uniqBy"' has no default export.
...but the project will build. And everything is fine. If you import an operator like this:
import uniqBy from 'lodash';
then you'll be importing the whole of lodash which is definitely not what you want.