0

I am trying lodash uniqBy function to filter in my Angular 2 application. But, I am getting error in import statement import * as _ from 'lodash';. I tried import _ from 'lodash';, import _ from 'lodash/lodash';, import * as _ from 'lodash/lodash'; also, but getting the same error i.e. Cannot find module 'lodash'. After checking angular2 failing lodash import, and Importing lodash into angular2 + typescript application, I mapped lodash to my system.config like so

<script>
        System.config({
        packages: {        
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        },
        map: {
          lodash: 'node_modules/lodash/lodash.js'
        }
      });
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>

I have also added lodash to the dependencies in my package.json file like so

"dependencies": {
        "angular2": "2.0.0-beta.15",
        "systemjs": "0.19.26",
        "es6-shim": "^0.35.0",
        "reflect-metadata": "0.1.2",
        "rxjs": "5.0.0-beta.2",
        "zone.js": "0.6.10",
        "bootstrap": "^3.3.6",
        "lodash":"^4.13.1"
    }
After npm install, I see lodash.js file here-->node_modules/lodash/lodash.js, but even then I am getting the error Cannot find module 'lodash'.

Please share your thoughts on what might be going wrong here.

Community
  • 1
  • 1
abhi
  • 349
  • 2
  • 8
  • 24

1 Answers1

0

In fact, there are two parts in TypeScript application:

  • The compilation part with type checking
  • The execution part when code is actually executed

By referencing the Lodash library in the SystemJS configuration, you tackle the execution part only. Your error comes from the compilation part since you missed to install typings for the Lodash library.

You could try the following:

$ typings install lodash --ambient
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • ambient flag appeared to be deprecated, so had to use global. It still didn't work. Got timeout message and ended with error. typings ERR! message Unable to connect to "https://api.typings.org/entries/npm/l odash/versions/latest" typings ERR! caused by connect ETIMEDOUT 104.24.113.177:443 typings ERR! node -v v4.4.5 typings ERR! typings -v 1.3.1 typings ERR! code EUNAVAILABLE – abhi Jul 14 '16 at 21:56
  • Apparently I didn't have to go through all these steps. Just referencing lodash in index.html was enough. And since lodash is not strictly typed, declaring a variable declare var _: any in app.ts worked. – abhi Jul 14 '16 at 22:16