Reputation: 3794
I'm confused. I installed Mocha globally via NPM and it is available in my path since I can run mocha
from the command line (Windows).
I've also installed Chai globally, but Node cannot find it (Error: Cannot find module 'chai'
).
Since they are both installed in the same way, why can't they both be accessed globally, and why are all the sites I've researched saying I need to install Chai locally?
Upvotes: 0
Views: 1501
Reputation: 151411
why are all the sites I've researched saying I need to install Chai locally?
Both Mocha and Chai should be installed locally so that:
You eliminate potential conflicts with other packages you use that need Mocha and Chai.
You specify in your package.json
which versions of Mocha and Chai people who contribute to your project should have installed in order to run the tests.
The fact of the matter is that both Mocha and Chai are still evolving: new features are added and with new features come new bugs. At various points in the past, I've had to pin the version of Mocha or Chai that some of my packages were using because of bugs. For instance, Mocha 2.4.x would work fine with my test suite but Mocha 2.5 introduced a bug that caused my tests to fail. The fastest way to work around the problem was to pin the version of Mocha to use with my project to any version higher or equal to 2.4.0 but lower than 2.5 and wait until they produced a fix in 2.5. (That's a real situation that really happened. I don't recall the exact version numbers though.) The same has happened with Chai at various points in its history.
When a release is made with a new major version (2.x -> 3.x, for instance) it is expected that code that used to work with the old version may not work with the new version. If you have one package that needs 2.x but won't work with 3.x and another that needs 3.x but won't work with 2.x and they try to use the global installation of the package, that's a problem. If instead they use locally installed version, they can use whatever version they need.
I cannot reproduce the error message you are getting. I can install Mocha locally for a project and have it find the global Chai without issue.
Upvotes: 1