0

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?

Robin Andrews
  • 3,514
  • 11
  • 43
  • 111
  • can you show us some of your code where you're getting 'chai not found' error? – Mukul Jain Apr 01 '17 at 15:30
  • `var chai = require('chai');` Apparently globally installed modules are not available to Node by default. Why do so many articles say to load Mocha globally and Chai locally? Is linking the answer? Is there a really good reason to to have multiple copies of the Chai package on the same machine? – Robin Andrews Apr 01 '17 at 16:22

1 Answers1

1

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:

  1. You eliminate potential conflicts with other packages you use that need Mocha and Chai.

  2. 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.

Louis
  • 146,715
  • 28
  • 274
  • 320
  • 1
    Regardless of what I should do, what I want to do is have Mocha and Chai installed globally and working together as one unit. Just like with PHPUnit, I can put `use PHPUnit\Framework\TestCase;` in my test files and hey presto I have unit testing available. – Robin Andrews Apr 08 '17 at 11:42
  • Mocha can be installed either locally or globally, but Chai can only be installed locally. Has to do with the way it is applied (i.e., to the specific app instance). – Steve Carey May 30 '20 at 21:11