Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Unit Testing Behavior Driven Development with Mocha & Chai Getting Started with Mocha and Chai

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Mocha not installing after running npm install

I'm not sure what it is I;m doing wrong here, maybe a problem with my package.json file?

$ npm install --save-dev mocha chai
npm WARN package.json npm@0.0.1 No repository field.
npm WARN package.json npm@0.0.1 No README data
npm WARN deprecated jade@0.26.3: Jade has been renamed to pug, please install the latest version of pug instead of jade
chai@3.5.0 ..\node_modules\chai
├── assertion-error@1.0.1
├── type-detect@1.0.0
└── deep-eql@0.1.3 (type-detect@0.1.1)

mocha@2.5.3 ..\node_modules\mocha
├── escape-string-regexp@1.0.2
├── diff@1.4.0
├── growl@1.9.2
├── commander@2.3.0
├── supports-color@1.2.0
├── to-iso-string@0.0.2
├── 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.11 (inherits@2.0.1, minimatch@0.3.0)

And then the console returns

$ mocha
bash: mocha: command not found

When trying to return the empty test. What's happening? :-)

5 Answers

akak
akak
29,445 Points

You need to install mocha globally to be able to run it from command line.

 sudo npm install -g mocha
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Thanks,

I wonder why it wasn't done this way in the video yet that's the exact answer in the code challenge. :-)

By the way I was able to run a test with Mocha using

npm test.

Not sure why this is :-)

You can fix NPM permissions so you don't have to type sudo before installing an node module globally. https://docs.npmjs.com/getting-started/fixing-npm-permissions

Robert Schaap
Robert Schaap
19,836 Points

As per akak's answer you need to install mocha globally. If you don't want to do that and you ran npm init before, just go into your package.json and change the test script to:

  "scripts": {
    "test": "mocha main_test.js"
  },

Then run npm test from the command line and it should work.

Gabbie Metheny
Gabbie Metheny
33,778 Points

You can also put "test": "mocha" under scripts in your package.json without a specific file. If you have a test folder in the root of your project, running npm test will default to using the files in that folder. Otherwise you can append your file path at the end of the command, i.e. npm test main_test.js.

Mocha Docs - Getting Started

NPM installs binaries to the node_modules/.bin folder. When you install mocha locally, and try to run it from the root of your project, your computer obviously doesn't see a binary called mocha.

Installing a package globally adds it to a directory in your PATH, which means your computer will look through the directories in your PATH until it finds it.

When running an NPM script (like npm test), NPM will know to look in the node_modules/.bin folder.

Alecia Mason
Alecia Mason
1,949 Points

In case you don't want to install it globally, and follow the video closer, before run npm install mocha --save-dev, you should run npm init. This creates a package.json file at the root of your project which has meta data like the project's dependancies.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Thanks Alecia,

I'll make a note of this and try installing it as save--dev again the next time i try unit testing. But it works just as well when i type npm. In fact when i caught up just now it seemed to be the only way to run a test. And all tests in the project.

Head London
Head London
1,716 Points

Im having issues with this as well...

in..

/Users/USER/workspace/TREEHOUSE/Unit-testing/battleship-and-tests

i ran

npm init

which gives me

USERS-MacBook-Pro:battleship-and-tests sam$ ls
node_modules    package.json

then

npm install --save-dev mocha chai

and get nothing when running mocha

USERS-MacBook-Pro:battleship-and-tests sam$ mocha
-bash: mocha: command not found
MAURO MARTINEZ
MAURO MARTINEZ
3,089 Points

i had to install mocha gloabally...

Rebekah Shaw
Rebekah Shaw
18,687 Points

I'm having the same problem