aaa aaa - 2 months ago 15x
Node.js Question

Node.js: Cannot find module 'chai'

I am trying to follow a first example from

Done this

$ npm install -g mocha


C:\Windows\system32>npm install -g mocha
npm WARN deprecated jade@0.26.3: Jade has been renamed to pug, please install th
e latest version of pug instead of jade
npm WARN deprecated graceful-fs@2.0.3: graceful-fs version 3 and before will fai
l on newer node releases. Please update to graceful-fs@^4.0.0 as soon as possibl
C:\Users\TestUser\AppData\Roaming\npm\_mocha -> C:\Users\TestUser\AppData\Roamin
C:\Users\TestUser\AppData\Roaming\npm\mocha -> C:\Users\TestUser\AppData\Roaming
mocha@2.4.5 C:\Users\TestUser\AppData\Roaming\npm\node_modules\mocha
├── escape-string-regexp@1.0.2
├── growl@1.8.1
├── commander@2.3.0
├── diff@1.4.0
├── supports-color@1.2.0
├── debug@2.2.0 (ms@0.7.1)
├── mkdirp@0.5.1 (minimist@0.0.8)
├── jade@0.26.3 (commander@0.6.1, mkdirp@0.3.0)
└── glob@3.2.3 (inherits@2.0.1, graceful-fs@2.0.3, minimatch@0.2.14)

Also installed chai (sorry, I originally forgot to mention it)

C:\Windows\system32>npm install -g chai
chai@3.5.0 C:\Users\TestUser\AppData\Roaming\npm\node_modules\chai
├── assertion-error@1.0.1
├── type-detect@1.0.0
└── deep-eql@0.1.3 (type-detect@0.1.1)

And here is the code

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));

Keep getting

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)

What am I doing wrong?


You have installed chai globally (with -g option), that's why require don't find it.

You need to install it locally (on your node_modules directory), so that require can find it.

To do so, type:

npm install --save-dev chai