How do you install and run Mocha, the Node.js testing module? Getting "mocha: command not found" after install

unit testing node.js express
mocha test
mocha no test files found
mocha multiple test files
mocha test naming convention
mocha test file structure
mocha test report
mocha data driven tests

I'm having trouble getting Mocha to work as expected, and I'd love to say as documented, but there (appears) to not be much documentation on actually getting the thing running.

I've installed it using npm (both globally and locally), and each time when I run it I get:

$ mocha
mocha: command not found

Ok, so I figured it's not in my PATH, so I tried running it directly,

$ ./node_modules/mocha/bin/mocha 
execvp(): No such file or directory

Finally, I tried hitting the other bin file, and got,

$ ./node_modules/mocha/bin/_mocha 
path.existsSync is deprecated. It is now called `fs.existsSync`.


  ✔ 1 tests complete (1ms)

How can I just execute my tests with a single command? Vows seems to let you, but I've heard Mocha is the better choice, I just can't seem to get it working correctly.

And any thoughts on the error I got above in my third attempt?


I'm running,

  • Ubuntu 11.10 64-bit
  • Node.js 0.7.5
  • npm 1.1.8
  • mocha 0.14.1
  • should 0.6.0

since npm 5.2.0, there's a new command "npx" included with npm that makes this much simpler, if you run:

npx mocha <args>

Note: the optional args are forwarded to the command being executed (mocha in this case)

this will automatically pick the executable "mocha" command from your locally installed mocha (always add it as a dev dependency to ensure the correct one is always used by you and everyone else).

Be careful though that if you didn't install mocha, this command will automatically fetch and use latest version, which is great for some tools (like scaffolders for example), but might not be the most recommendable for certain dependencies where you might want to pin to a specific version.

You can read more on npx here

Now, if instead of invoking mocha directly, you want to define a custom npm script, an alias that might invoke other npm binaries...

you don't want your library tests to fail depending on the machine setup (mocha as global, global mocha version, etc), the way to use the local mocha that works cross-platform is:

node node_modules/.bin/mocha

npm puts aliases to all the binaries in your dependencies on that special folder. Finally, npm will add node_modules/.bin to the PATH automatically when running an npm script, so in your package.json you can do just:

"scripts": {
  "test": "mocha"

and invoke it with

npm test

Test Execution Order in Mocha, Mocha is a feature-rich JavaScript test framework running on Node.js and in the Installation; Getting Started; Run Cycle Overview; Detects Multiple Calls to In the above example, we're using Node.js' built-in assert module  Testing is an integral part of software development. With the right test setup, this process can be automated, saving a lot of time. In this article, you'll write tests for a Node.js TODO list module. You will set up and use the Mocha test framework t

Mocha, Mocha is a simple, extensible and fast testing library for Node.js. This article will walk you through its installation, configuration and usage. Npm's init command-​line option will launch a wizard, which creates a package.json for our project. npm test module.js:340 throw err; ^ Error: Cannot find module '. Our project’s structure will contain an app directory that with the main code, and a test directory with the Mocha test files: $ mkdir app $ mkdir test. Next, we will install our testing framework, and an expectation library called Chai that serves as a nice replacement for Node’s standard assert function. $ npm install --save mocha chai

For windows :


  "scripts": {
    "start": "nodemon app.js",
    "test": "mocha"

then run the command

npm run test

Getting Started with Node.js and Mocha, Before we run our Mocha express tests, however, we shall begin with creating If you've never worked with Node.js before, start with installing the npm //Load express module with `require` directive var express = require('express') var app in root URL (/) app.get('/', function (req, res) { res.send('Hello World') }) //Launch  Creating a Hello World website with Node.js Install Node.js Install NPM and Mocha Create Hello World with Express framework Details of Hello World Run the app Configuring unit tests with Mocha and Chai Install Mocha and Chai Add a test file Grouping tests Automating Mocha and Chai tests Version control Continuous Integration with Buddy

How to run Mocha/Chai tests on Node.js apps, How do you install and run Mocha, the Node.js testing module? Getting “mocha: command not found” after install - node.js. That would not only be a massive misuse of time - but unreliable as well. Hence the need for setting up a testing framework for automating the testing of the application, so you can run hundreds of tests in a matter of seconds. With that, install Mocha: $

How To Test a Node.js Module with Mocha and Assert, To compare values in a test, we can use the Node.js assert module. In this article To download and install Mocha, use the following: Let's verify that we've stored our "run code" TODO item by getting a list of all our TODOs:. Creating a Hello World website with Node.js. We’ll start with creating a simple Node.js application, just like we usually do. The subsequent part of this article will explain how to write, run, and automate tests with BuddyWorks. Install Node.js

A quick and complete guide to Mocha testing, Mocha is a JavaScript test runner that runs both on Node.js and in the browser. To complete your Mocha setup, you will have to write a unit test for a If you are using Mocha in a Node.js environment, you can use the built-in assert module as your You should get an output that looks like the following:. You can now either setup your tests in a test/ directory or have a simple test.js file in case you only have a few tests to run. Also, don't forget to have a mocha.opts file with your options . This should work, especially if you're using Git Bash (I tried on windows CMD and it works too!).

How to run Mocha/Chai unit tests on Node.js apps, If that's the first time you work with Node.js, install the npm manager first: nodejs.​org/en/download/package- //Load express module with `require` directive Here, we will use Mocha as the test running framework, and Chai as the assertion library. Subscribe to get your daily round-up of top tech stories! simple, flexible, fun. Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun.Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.

  • With node 0.6.12, npm 1.1.4, and executing npm install mocha, I get ./node_modules/.bin/mocha as expected. sudo npm install -g mocha gets me /usr/local/bin/mocha. If this doesn't work properly in your environment, it might be a bug, or just mocha not updated to support node 0.7.*. Regarding your deprecation warning, exists and existsSync were moved from path to fs in node 0.7.1.
  • I downgraded to Node.js 0.6.12, added "./node_modules/.bin" to my PATH, and all is right with the world.
  • +1, I have been using mocha from sometime ago, but I did't realize it. This should be the accepted answer!
  • @michael.kebe no, you don't need to specify that path to use the local mocha, npm already adds "./node_modules/.bin" to the $PATH, so it will use the local version by default.
  • I believe that the command should be node node_modules/mocha/bin/mocha not node node_modules/.bin/mocha
  • @MinaLuke that works too but it's a bad idea, /node_modules/.bin/{module-binary-name} is the place where npm puts shortcuts to binaries in your dependencies, you shouldn't reference directly a file in the sources of a dependency, as that might break on the next version.
  • @DougBarbieri nice catch, it's an old answer, I'll update to mention npx, thanks!
  • I have node 0.8.12 and mocha both globally and locally installed (for different projects), and they are working. So no need to downgrade node. For locally installed mocha, just make sure your PATH is set as above, or call it directly from your script (Makefile or "scripts" tag).
  • I had Mocha installed locally and globally but it wasn't found in the path (Win8). I had to manually add the './node_modules/.bin' folder relative to my project to my PATH, restart console then typing 'mocha' would work. Not ideal, but a quick hack to get it working. Perhaps if you had a top level location to install mocha locally, then link to that PATH so you could use it elsewhere.