Collin Estes
Collin Estes

Reputation: 5789

mocha running with NPM test but not regular mocha CLI command

I am trying to understand what I am doing wrong in this instance. I have a Node.js project with the following in my package.json

  "scripts": {
    "test": "mocha --recursive ./src/setup/*.js ./test/**/*.js"
  },
  "dependencies": {
     "mocha": "^2.2.5"
  }

When I run 'npm test' the mocha tests are run correctly:

$ npm test (successful run)

However when I try to just run the mocha command I have there in my package.json

$ mocha --recursive ./src/setup/*.js ./test/**/*.js"

This errors with:

-sh: mocha: command not found

I do not have mocha globally installed, I only have installed it via npm to this specific project.

If I install mocha globally then it works. Why doesn't it work when I have simply have mocha installed in the current directory's node_modules, yet it does with 'npm test'?

Upvotes: 6

Views: 3166

Answers (1)

beautifulcoder
beautifulcoder

Reputation: 11330

npm scripts automatically add mocha to the PATH:

If you depend on modules that define executable scripts, like test suites, then those executables will be added to the PATH for executing the scripts.

https://docs.npmjs.com/misc/scripts#path

Upvotes: 8

Related Questions