最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does mocha need to be in devDependencies? - Stack Overflow

programmeradmin0浏览0评论

On the face of it, mocha being in devDependencies like the tutorials say, is logical enough, it is after all a dev dependency.

But in practice you install it -g so you can run mocha as a mand. And as far as I can tell, given that, it makes no difference at all whether it's mentioned in your package.json.

So is there any need to explicitly list it?

On the face of it, mocha being in devDependencies like the tutorials say, is logical enough, it is after all a dev dependency.

But in practice you install it -g so you can run mocha as a mand. And as far as I can tell, given that, it makes no difference at all whether it's mentioned in your package.json.

So is there any need to explicitly list it?

Share Improve this question asked Apr 21, 2017 at 11:04 rwallacerwallace 33.6k44 gold badges134 silver badges279 bronze badges 1
  • 1 you can call mocha executable from node modules, or work with mocha configuration programmatically, also it locks the version – Gntem Commented Apr 21, 2017 at 11:07
Add a ment  | 

3 Answers 3

Reset to default 5

If you're working on an open-source project, one of your goals could be to allow other developers to be able to start contributing quickly.

One of the things that will help a lot is the possibility for a new developer to quickly be able to build and run your project, as well as run the tests. In order to do that, you can provide an easy way of installation of all the tools that a developer should have in order to contribute to your project.

This includes:

  • Build tools
  • Testing tools
  • Code quality tools (linter)

On the other hand, a user of your project is likely not going to need any of that, which is a good reason to split dependencies and devDependencies.


On top of that, it's useful to edit your package.json to provide useful scripts so that you can, for example, run npm test. It's mon to specify something like:

{
    ...
    "scripts": {
        ...
        "test": "mocha -opts mocha.opts ...tests..."
    }
}

Then npm test is going to run the specific mocha from your node_modules.

If you install it globally, that's a single version across all your projects.

If it's a dev dependency, each project can be using a version specific to that project, and the project can migrate to newer versions in a controlled way.

Pretty much the same argument as for having other modules loaded project-specific rather than globally.

Because you don't need to run mocha as a mand. You can run it from node_modules like so: ./node_modules/.bin/mocha.

Npm has special support for this. If you have the following in package.json:

"scripts": {
  "test": "mocha"
},
 "devDependencies": {
  "mocha": "*"
}

Then you can execute npm test even if you don't have mocha globally installed.

So, what's the use of this? First of all it's a nice thing to do if you collaborate with other developers - they don't need to do anything more than npm install to set up the development environment.

Second, and I think more useful, is this makes it easy to integrate your project with other tools like Travis etc.

发布评论

评论列表(0)

  1. 暂无评论