2016-04-25 98 views
9

我試圖遵循https://mochajs.org/的Node.js:找不到模塊 '柴'

第一個例子做完這

$ npm install -g mocha 

C:\Windows\system32>npm install -g mocha 
npm WARN deprecated [email protected]: Jade has been renamed to pug, please install th 
e latest version of pug instead of jade 
npm WARN deprecated [email protected]: graceful-fs version 3 and before will fai 
l on newer node releases. Please update to [email protected]^4.0.0 as soon as possibl 
e. 
C:\Users\TestUser\AppData\Roaming\npm\_mocha -> C:\Users\TestUser\AppData\Roamin 
g\npm\node_modules\mocha\bin\_mocha 
C:\Users\TestUser\AppData\Roaming\npm\mocha -> C:\Users\TestUser\AppData\Roaming 
\npm\node_modules\mocha\bin\mocha 
[email protected] C:\Users\TestUser\AppData\Roaming\npm\node_modules\mocha 
├── [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] ([email protected], [email protected], [email protected]) 

還安裝柴(抱歉,我原本忘了提及它)

C:\Windows\system32>npm install -g chai 
[email protected] C:\Users\TestUser\AppData\Roaming\npm\node_modules\chai 
├── [email protected] 
├── [email protected] 
└── [email protected] ([email protected]) 

這裏是代碼

var assert = require('chai').assert; 
describe('Array', function() { 
    describe('#indexOf()', function() { 
    it('should return -1 when the value is not present', function() { 
     assert.equal(-1, [1,2,3].indexOf(5)); 
     assert.equal(-1, [1,2,3].indexOf(0)); 
    }); 
    }); 
}); 

不斷得到

Error: Cannot find module 'chai' 
at Function.Module._resolveFilename (module.js:325:15) 
at Function.Module._load (module.js:276:25) 
at Module.require (module.js:353:17) 
at require (internal/module.js:12:17) 
at Object.<anonymous> (c:\git\develop\SendText\test\test2.js:1:76) 
at Module._compile (module.js:409:26) 
at Object.Module._extensions..js (module.js:416:10) 
at Module.load (module.js:343:32) 
at Function.Module._load (module.js:300:12) 
at Function.Module.runMain (module.js:441:10) 

我在做什麼錯?

回答

24

您已經安裝chai全球(與-g選項),這就是爲什麼require並不覺得。

您需要在本地安裝(在您的node_modules目錄中),以便require可以找到它。

要做到這一點,類型:

npm install --save-dev chai 
+0

chai已安裝,但仍未看到我恐怕 – aaa

+1

您已經在全局安裝了'chai'(使用'-g'),您必須將它安裝在您的項目目錄的本地。 – Blackus

+0

謝謝。這工作。但是,全球安裝東西有什麼意義呢? – aaa

1

您需要在本地安裝chai來需要它。

npm install chai 
+0

chai是安裝好了,但還是沒有看到,恐怕 – aaa

+0

柴安裝成全局包,但要求它需要安裝它本地。您可以使用[documentation](https://nodejs.org/api/modules.html#modules_all_together)瞭解如何在Node.js中運行'require'。 –

0

您應該安裝柴

$ npm install --save chai

+0

柴安裝,但仍然沒有看到我害怕 – aaa

0

你需要確保兩個設備都在同一個文件夾 因爲如果node_modules文件夾(在摩卡安裝期間創建) 不在您要安裝chi的相同文件夾中, 然後錯誤:找不到模塊'柴'出現。

NPM安裝--global摩卡

NPM安裝卡

NPM測試(測試成功執行)

0

NPM安裝類型/柴

安裝在本地驅動器