0

I have an app written in ionic2 and I'm using lodash. With a simple npm install lodash on the command line and import * as _ from 'lodash' and I can access lodash via _ with no issues

I've started a new app in angular2 cli, which is what ionic is built on, and I've tried to do the same thing. i've run npm install lodash on the command line and import * as _ from 'lodash' but once I try to compile the application I get ERROR in {DIRECTORY_PATH}/src/app/otherlists/otherlists.component.ts (2,20): Cannot find module 'lodash'. and I can't seem to get it working. I've even copied the lodash folder from the node_modules and its no different..

What am I missing?

EDIT,

to demonstrate this further, I've created a brand new app from scratch. ng new parser this creates a new angular project. Next command is npm install --save lodash and I import the library into my project with import * as _ from 'lodash'; Once I try and run it with ng serve I get the following error:

ERROR in C:/dev/ionic/parser/src/app/mainpage/mainpage.component.ts (2,20): Cannot find module 'lodash'.

If I run npm install --save-dev @types/lodash I get a long list of errors starting with

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8604,32): ',' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8607,14): ']' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8607,15): ';' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8610,26): ',' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8610,42): '(' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8610,61): ',' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8610,66): ')' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8612,15): ';' expected.

ERROR in C:/dev/ionic/parser/node_modules/@types/lodash/index.d.ts (8612,25): Declaration or statement expected.

For posterity, here's my package.json

{
  "name": "parser",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "test": "ng test",
    "pree2e": "webdriver-manager update --standalone false --gecko false",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^2.3.1",
    "@angular/compiler": "^2.3.1",
    "@angular/core": "^2.3.1",
    "@angular/forms": "^2.3.1",
    "@angular/http": "^2.3.1",
    "@angular/platform-browser": "^2.3.1",
    "@angular/platform-browser-dynamic": "^2.3.1",
    "@angular/router": "^3.3.1",
    "core-js": "^2.4.1",
    "lodash": "^4.17.4",
    "rxjs": "^5.0.1",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.7.2"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^2.3.1",
    "@types/jasmine": "2.5.38",
    "@types/lodash": "^4.14.71",
    "@types/node": "^6.0.42",
    "angular-cli": "1.0.0-beta.28.3",
    "codelyzer": "~2.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "~4.0.13",
    "ts-node": "1.2.1",
    "tslint": "^4.3.0",
    "typescript": "~2.0.3"
  }
}
3
  • npm install lodash only install it, think it needs to be in dependancy for the typescript compiler to import it, try npm install -S lodash Commented Jul 29, 2017 at 0:19
  • Aaready tried that with no difference Commented Jul 29, 2017 at 0:22
  • take look @ this link It has question on how to import third part JS with or without typings Commented Jul 29, 2017 at 20:20

1 Answer 1

2

You need to install both the package and the typings:

npm install --save lodash and npm install --save-dev @types/lodash

You will then be able to access the types in the normal way using import * as _ from 'lodash'

This github repository shows an example of use with Angular 4 and Angular CLI 1.3.

Sign up to request clarification or add additional context in comments.

8 Comments

I've tried it and it still gives more errors with the typings install. If I remove the typings, I should be able to access lodash but I can't. Like I sat, it works on my ionic project, which is esentially the same
I've even removed and re-installed node modules but no difference
Update your question with what you've tried and the corresponding error messages
I've added another example in the question
Another note: Chances are you'll have to use an older version of lodash and its typings if you keep "angular-cli": "1.0.0-beta.28.3" in your project. Official Angular-CLI is only @angular/cli and @angular/compiler-cli. This other npm item is likely the root of your issues.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.