2017-01-08 301 views
3

我被卡住並尋求幫助。找不到模塊:錯誤:無法解析'@ types/lodash'模塊

我不確定問題的實際位置,但我希望找到答案。用我的.ts文件

npm install --save-dev @types/lodash 

而且進口lodash:

import * as _ from '@types/lodash'; 

當我

問題的詳細信息

我已經安裝了 '@類型/ lodash' 使用下面的命令運行webpack我得到以下錯誤:

ERROR in ./app/scripts/Components/GameBoard.ts 
Module not found: Error: Cannot resolve module '@types/lodash' in /Users/mjelaska/Documents/DevProjects/connect-four/src/app/scripts/Components 
@ ./app/scripts/Components/GameBoard.ts 3:8-32 

然後我試圖與

webpack --display-error-details 

欲瞭解詳細內容,並得到⇣

ERROR in ./app/scripts/Components/GameBoard.ts 
Module not found: Error: Cannot resolve module '@types/lodash' in /Users/mjelaska/Documents/DevProjects/connect-four/src/app/scripts/Components 
resolve module @types/lodash in /Users/mjelaska/Documents/DevProjects/connect-four/src/app/scripts/Components 
    looking for modules in /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules 
    resolve 'file' or 'directory' lodash in /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types 
     resolve file 
     /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.webpack.js doesn't exist 
     /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash is not a file 
     /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.web.js doesn't exist 
     /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.ts doesn't exist 
     /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.js doesn't exist 
     resolve directory 
     directory default file index 
      resolve file index in /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash 
      /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index doesn't exist 
      /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.webpack.js doesn't exist 
      /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.web.js doesn't exist 
      /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.ts doesn't exist 
      /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.js doesn't exist 
     use from package.json 
      resolve 'file' or 'directory' /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash 
      resolve file 
       /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash is not a file 
       /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.webpack.js doesn't exist 
       /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.web.js doesn't exist 
       /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.ts doesn't exist 
       /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.js doesn't exist 
      resolve directory 
       use from package.json 
       abort resolving because of recursion 
       directory default file index 
       resolve file index in /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash 
        /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index doesn't exist 
        /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.webpack.js doesn't exist 
        /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.web.js doesn't exist 
        /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.ts doesn't exist 
        /Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.js doesn't exist 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.webpack.js] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.web.js] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.ts] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.js] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.webpack.js] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.web.js] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.ts] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.js] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.webpack.js] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.web.js] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.ts] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash.js] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.webpack.js] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.web.js] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.ts] 
[/Users/mjelaska/Documents/DevProjects/connect-four/src/node_modules/@types/lodash/index.js] 
@ ./app/scripts/Components/GameBoard.ts 3:8-32 

設置細節

的package.json(W/O一些無關痛癢的信息)

{ 
    "devDependencies": { 
    "@types/lodash": "^4.14.47", 
    "@types/node": "^6.0.55", 
    "@types/pixi.js": "^4.3.1", 
    "npm": "^4.0.5", 
    "ts-loader": "^1.3.3", 
    "typescript": "^2.1.4", 
    "webpack": "^1.14.0" 
    } 
} 

tsconfig.json

{ 
    "compilerOptions": { 
     "module": "commonjs", 
     "target": "es5", 
     "noImplicitAny": true, 
     "sourceMap": false, 
     "removeComments": true, 
     "typeRoots": [ 
      "./node_modules/@types" 
     ] 
    }, 
    "files": [ 
     "./app/scripts/main.ts" 
    ] 
} 

webpack.config.js

module.exports = { 
    entry: './app/scripts/main.ts', 
    output: { 
     filename: './build/scripts/main.js' 
    }, 
    resolve: { 
     extensions: ['', '.webpack.js', '.web.js', '.ts', '.js'] 
    }, 
    module: { 
     loaders: [ 
      { 
       test: /\.ts$/, 
       loader: 'ts-loader' 
      } 
     ] 
    } 
}; 

環境的詳細信息

node: v6.9.4 
npm: v4.0.5 
webpack: v1.14.0 (as seen in `package.json`, installed also globally) 

項目的時刻,當出現問題:https://github.com/MiroslavJelaska/connect-four/tree/4941ca792096602831940f76fa350e461808a8c5

⬆️更新#1

安裝lodash依賴關係inst devDependencies的EAD沒有幫助:

npm install --save @types/lodash 
+0

難道僅僅是因爲我」我從來沒有見過它,但據我所知,你只需以這種方式安裝lodash(根據我所看到/完成的內容以及npm doc上的內容): $ npm i -g npm $ npm i - 保存lodash 你只需輸入「lodash」。「@types」的用途是什麼?這似乎是在你的應用程序中拋出模塊分辨率。 –

+0

@TimConsolazio我試着用--save來安裝,但它沒有什麼區別。 「@types」是TypeScript類型定義的DefinitelyTyped項目的前綴。這裏是GitHub repo:https://github.com/DefinitelyTyped/DefinitelyTyped – kr85

+0

你是否嘗試過使用基本命令「npm install lodash --save-dev」來安裝它,然後直接導入它,而沒有DefinitelyTyped東東。也許DefinitelyTyped的東西(我一無所知)正在拋棄它。請注意,這些細節(你正在做的除了基本安裝之外的東西)會有所幫助,這實際上可能是DefinitelyTyped(無論是/應該做的)某種問題。 –

回答

4

您需要的實際lodash包(其中包含JS代碼,lodash),你還需要@types/lodash包,它提供了lodash封裝類型定義(本質上,它會告訴TypeScript lodash包中的內容)。

所以,你要同時運行這些命令來獲得這兩個包:

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

然後在你的代碼,你導入它是這樣:

import * as _ from 'lodash'; 

_.zip(array1, array1); // or whatever 
+0

所以我遇到了類似的問題'exceljs'我有'exceljs'和'@ types/exceljs',我的導入語句是'import * as exceljs'; Excel的Excel,但我仍然得到未找到模塊,無法解決錯誤... – Oliver

相關問題