1

Was hoping someone could point me in the right direction here? I am trying to add underscore.js to my AngularJS2 application, but cannot get past the error message "Cannot find module 'underscore'".

I've executed: npm install underscore --save and it adds the reference to my package.json file:

{
  "name": "angular-quickstart",
  "version": "1.0.0",
  "description": "QuickStart package.json from the documentation, supplemented with testing support",
  "scripts": {
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
    "docker-build": "docker build -t ng2-quickstart .",
    "docker": "npm run docker-build && docker run -it --rm -p 3000:3000 -p 3001:3001 ng2-quickstart",
    "pree2e": "npm run webdriver:update",
    "e2e": "tsc && concurrently \"http-server -s\" \"protractor protractor.config.js\" --kill-others --success first",
    "lint": "tslint ./app/**/*.ts -t verbose",
    "lite": "lite-server",
    "test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
    "test-once": "tsc && karma start karma.conf.js --single-run",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "webdriver:update": "webdriver-manager update"
  },
  "keywords": [],
  "author": "",
  "licenses": [
    {
      "type": "MIT",
      "url": "https://github.com/angular/angular.io/blob/master/LICENSE"
    }
  ],
  "dependencies": {
    "@angular/common": "~2.1.1",
    "@angular/compiler": "~2.1.1",
    "@angular/core": "~2.1.1",
    "@angular/forms": "~2.1.1",
    "@angular/http": "~2.1.1",
    "@angular/material": "^2.0.0-alpha.9-3",
    "@angular/platform-browser": "~2.1.1",
    "@angular/platform-browser-dynamic": "~2.1.1",
    "@angular/router": "~3.1.1",
    "@angular/upgrade": "~2.1.1",
    "ag-grid": "6.2.x",
    "ag-grid-ng2": "6.2.x",
    "angular-in-memory-web-api": "~0.1.13",
    "angular2-cool-storage": "^1.1.0",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "ng2-bootstrap": "^1.1.16",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "0.19.39",
    "underscore": "^1.8.3",
    "zone.js": "^0.6.26"
  },
  "devDependencies": {
    "concurrently": "^3.1.0",
    "lite-server": "^2.2.2",
    "typescript": "^2.0.3",
    "canonical-path": "0.0.2",
    "http-server": "^0.9.0",
    "tslint": "^3.15.1",
    "lodash": "^4.16.2",
    "jasmine-core": "~2.5.2",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-htmlfile-reporter": "^0.3.4",
    "karma-jasmine": "^1.0.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "^4.0.9",
    "rimraf": "^2.5.4",
    "@types/core-js": "^0.9.34",
    "@types/node": "^6.0.45",
    "@types/jasmine": "^2.5.35",
    "@types/selenium-webdriver": "^2.53.32"
  },
  "repository": {}
}

This is my systemjs.config.js file, as you can see I've added the reference there also:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'ag-grid-ng2': 'node_modules/ag-grid-ng2',
      'ag-grid': 'node_modules/ag-grid',
      'ag-grid-enterprise': 'node_modules/ag-grid-enterprise',
      '@angular/material': 'npm:@angular/material/material.umd.js',
      'angular2-cool-storage': 'npm:angular2-cool-storage',
      'underscore': 'npm:underscore',
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: { main: './main.js', defaultExtension: 'js' },
      rxjs: { defaultExtension: 'js' },
      'angular-in-memory-web-api': { main: './index.js', defaultExtension: 'js' },
      lib: { format: 'register', defaultExtension: 'js' },
      'ag-grid-ng2': { defaultExtension: "js" },
      'ag-grid': { defaultExtension: "js" },
      'angular2-cool-storage': { main: './cool-storage.js', defaultExtension: 'js' },
      'underscore': { main: './underscore.js', defaultExtension: 'js' },
      map: {
          'ag-grid-ng2': 'node_modules/ag-grid-ng2',
          'ag-grid': 'node_modules/ag-grid'
      }
    }
  });
})(this);

This is my tsconfig.json file:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "typeRoots": [ "./node_modules/@types/" ]
  },  
  "compileOnSave": true,
  "exclude": [ "node_modules/*", "**/*-aot.ts" ]
}

Now when I try to add:

import * as _ from 'underscore';

Into my component, I get the cannot find module underscore error message.

I've followed all online examples to the T and still getting this error.

Can anyone see what I've missed?

Thank you.

1
  • Did you take a look into node_modules to check, if underscore is there? Commented Nov 4, 2016 at 17:37

2 Answers 2

1

You have most likely installed underscore and your application would work at runtime, but you are missing the type declarations so the typescript compiler doesn't know about the module and complains about it.

To resolve this you need to install the underscore type definitions through typings or whatever tool you use for this.

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

2 Comments

That was it, i installed via:typings install dt~underscore --global --save, and it worked. Thank you sir.
npm install --save @types/underscore Is what you ppl are looking for. Thanks Matthias!
1

Install the typings too.

npm install --save underscore
npm install --save @types/underscore

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.