2015-11-02 87 views
0

我有一個非常簡單的腳本,用於測試此場景,但我無法弄清楚爲什麼Node無法加載本地安裝的模塊。在Windows中找不到本地安裝的模塊

的package.json:

{ 
    "name": "Test", 
    "version": "1.0.0", 
    "dependencies": { 
    "cli-progress-bar": "^0.1.0" 
    } 
} 

Test.js:

var ProgressBar = require("cli-progress-bar"), 
    progress = new ProgressBar(), 
    processed = 0; 

function update() { 
    if (processed < 100) { 
     progress.show(++processed/100); 
     setTimeout(update, 750); 
    } 
} 

update(); 

據我所知,這應該只是加載cli-progress-bar包,和之前運行一個簡單的進度條完成退出。

這是輸出我得到的,當我嘗試安裝的依賴關係和運行腳本:

C:\Users\Benjamin\Desktop\Test>node -v 
v4.2.1 

C:\Users\Benjamin\Desktop\Test>npm -v 
3.3.10 

C:\Users\Benjamin\Desktop\Test>npm install 
[email protected] C:\Users\Benjamin\Desktop\Test 
└─┬ [email protected] 
    ├── [email protected] 
    ├─┬ [email protected] 
    │ ├── [email protected] 
    │ └─┬ [email protected] 
    │ └── [email protected] 
    ├── [email protected] 
    └─┬ [email protected] 
    ├── [email protected] 
    └─┬ [email protected] 
     └─┬ [email protected] 
     ├── [email protected] 
     └── [email protected] 

npm WARN EPACKAGEJSON [email protected] No description 
npm WARN EPACKAGEJSON [email protected] No repository field. 
npm WARN EPACKAGEJSON [email protected] No license field. 

C:\Users\Benjamin\Desktop\Test>node Test.js 
module.js:339 
    throw err; 
    ^

Error: Cannot find module 'cli-progress-bar' 
    at Function.Module._resolveFilename (module.js:337:15) 
    at Function.Module._load (module.js:287:25) 
    at Module.require (module.js:366:17) 
    at require (module.js:385:17) 
    at Object.<anonymous> (C:\Users\Benjamin\Desktop\Test\Test.js:1:81) 
    at Module._compile (module.js:435:26) 
    at Object.Module._extensions..js (module.js:442:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:311:12) 
    at Function.Module.runMain (module.js:467:10) 

我可以看到,該軟件包安裝在C:\Users\Benjamin\Desktop\Test\node_modules但我似乎無法使用它。

C:\Users\Benjamin\Desktop\Test>dir node_modules 
Volume in drive C is OS 
Volume Serial Number is 7294-620F 

Directory of C:\Users\Benjamin\Desktop\Test\node_modules 

11/02/2015 10:00 AM <DIR>   . 
11/02/2015 10:00 AM <DIR>   .. 
11/02/2015 10:00 AM <DIR>   ansi-escapes 
11/02/2015 10:00 AM <DIR>   cli-character-set 
11/02/2015 10:00 AM <DIR>   cli-cursor 
11/02/2015 10:00 AM <DIR>   cli-progress-bar 
11/02/2015 10:00 AM <DIR>   exit-hook 
11/02/2015 10:00 AM <DIR>   lodash.padleft 
11/02/2015 10:00 AM <DIR>   lodash.padright 
11/02/2015 10:00 AM <DIR>   lodash.repeat 
11/02/2015 10:00 AM <DIR>   lodash._basetostring 
11/02/2015 10:00 AM <DIR>   lodash._createpadding 
11/02/2015 10:00 AM <DIR>   log-update 
11/02/2015 10:00 AM <DIR>   onetime 
11/02/2015 10:00 AM <DIR>   restore-cursor 
       0 File(s)    0 bytes 
       15 Dir(s) 283,532,754,944 bytes free 

C:\Users\Benjamin\Desktop\Test>npm ls 
[email protected] C:\Users\Benjamin\Desktop\Test 
└─┬ [email protected] 
    ├── [email protected] 
    ├─┬ [email protected] 
    │ ├── [email protected] 
    │ └─┬ [email protected] 
    │ └── [email protected] 
    ├── [email protected] 
    └─┬ [email protected] 
    ├── [email protected] 
    └─┬ [email protected] 
     └─┬ [email protected] 
     ├── [email protected] 
     └── [email protected] 

每次我嘗試運行腳本時,都會得到相同的錯誤。

+0

我搜索了其他人有此問題,但我只能找到加載*全球*安裝模塊時遇到問題的人的問題。這肯定是在本地安裝的。 – btleffler

+0

我不知道該從哪裏開始。如果每個人都認爲這是特定軟件包的問題,​​我會在其Github存儲庫中打開一個問題。 – btleffler

+0

因此,測試腳本所在的目錄中沒有node_modules文件夾? –

回答

5

你無法找到名爲「cli-progress-bar」的模塊的原因是因爲該模塊沒有正確暴露自身。 package.json文件有一個名爲main的屬性,它必須設置爲從模塊中導出的腳本的名稱。如果腳本被稱爲index.js,npm會找到它,但是因爲腳本被稱爲bar.js,而package.json文件則說主文件被稱爲index.js,它沒有正確公開並且不能被require找到。

+0

哇,很好。謝謝!那麼我會爲那個包打開一個問題。 – btleffler

+0

沒問題我已經做了PR [這裏](https://github.com/gillesdemey/cli-progress-bar/pull/2)來解決問題 –

相關問題