2016-11-04 62 views
0

希望有人能指點我在這裏的正確方向嗎?我試圖將underscore.js添加到我的AngularJS2應用程序中,但無法通過錯誤消息「Can not find module'underscore'」。AngularJS2 - 添加underscore.js - 找不到模塊'下劃線'

我執行:NPM安裝下劃線--save並將其添加引用我的package.json文件:

{ 
    "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": {} 
} 

這是我systemjs.config.js文件,你可以看到我已經添加引用也有:

​​3210

這是我tsconfig.json文件:

{ 
    "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" ] 
} 

現在,當我嘗試添加:

import * as _ from 'underscore'; 

在我的組件中,我得到了無法找到模塊的下劃線錯誤信息。

我已經按照所有在線的例子到T,仍然得到這個錯誤。

任何人都可以看到我錯過了什麼嗎?

謝謝。

+0

如果下劃線在那裏,你看看'node_modules'來檢查嗎? – pzaenger

+0

是的,它在那裏。 –

回答

1

您最有可能安裝了下劃線,並且您的應用程序可以在運行時工作,但是您缺少類型聲明,因此打字稿編譯器不知道該模塊並且抱怨它。

要解決此問題,您需要通過typings或任何其他用於此目的的工具安裝下劃線類型定義。

+0

就是這樣,我通過安裝:typings install dt〜underscore --global --save,它工作。謝謝你,先生。 –

+0

'npm install --save @ types/underscore'是你正在尋找的東西。感謝Matthias! – codepleb

0

也安裝打字。

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