2016-04-25 63 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安装类型/柴

安装在本地驱动器