0

I am running the latest version of the angular2 cli 1.0.0-rc.4 and I am having a problem with one of my libraries.

When I try to import the lodash library (or any library) with

import * as _ from 'lodash';

I will get the following error when building:

ERROR in C:/tfsrepos/Nexus/Nexus/my-app/src/app/app.component.ts (2,20): Cannot find module 'lodash
.)

If I instead try to import using the line

import 'lodash';

then it builds fine. However, now I can't use lodash because I haven't gotten it installed in any namespace. The following line

_.fill(array, 'a');

produces the error

Cannot find name '_'.

What is the issue here? My code works fine before I started trying to build using angular cli.

None of the solutions mentioned in this thread: Importing lodash into angular2 + typescript application work for me.

Jason Cheng
  • 180
  • 2
  • 12
  • 1
    Possible duplicate of [Importing lodash into angular2 + typescript application](http://stackoverflow.com/questions/34660265/importing-lodash-into-angular2-typescript-application) – Paolo Mar 30 '17 at 17:35
  • I had seen that before but when ran npm install --save @types/lodas, I get more than a hundred errors building. ERROR in C:/tfsrepos/Nexus/Nexus/my-app/node_modules/@types/lodash/index.d.ts (11502,21): ']' expect ed.) C:/tfsrepos/Nexus/Nexus/my-app/node_modules/@types/lodash/index.d.ts (11502,22): ';' expected.) C:/tfsrepos/Nexus/Nexus/my-app/node_modules/@types/lodash/index.d.ts (11502,23): Declaration or stat ement expected.) C:/tfsrepos/Nexus/Nexus/my-app/node_modules/@types/lodash/index.d.ts (11502,33): ']' expected.) ... – Jason Cheng Mar 30 '17 at 19:35
  • My co-worker has found the solution to this problem. I am surprised that no one has mentioned it before on the web. I guess lodash isn't very popular. The problem is due to angular cli using a lower version of typescript than what the lodash typings package was built with. The solution is to force an older version of the typings to be used. In packages.json, under the devDependencies seciont, include @types/lodash": "ts2.0". Delete and reinstall the lodash typings via npm. This solves the problem. – Jason Cheng Apr 10 '17 at 15:11

2 Answers2

0

Try use _ directly before from.

import _ from 'lodash';

rafaedez
  • 56
  • 5
0

Use :
npm i --save-dev @types/lodash

and you can import in your file :

      import * as _ from 'lodash';
Merzouk MENHOUR
  • 107
  • 1
  • 5