[英]Why does mocha need to be in devDependencies?
On the face of it, mocha being in devDependencies like the tutorials say, is logical enough, it is after all a dev dependency. 从表面上看,mocha像教程中所说的那样处于devDependencies,这是合乎逻辑的,毕竟是dev的依赖。
But in practice you install it -g so you can run mocha as a command. 但实际上,您可以将其安装在-g位置,以便可以将mocha作为命令运行。 And as far as I can tell, given that, it makes no difference at all whether it's mentioned in your package.json. 据我所知,无论是否在package.json中提到它,都没有任何区别。
So is there any need to explicitly list it? 那么有必要明确列出它吗?
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: 这包括:
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
. 另一方面,您项目的用户可能不需要这些,这是拆分dependencies
和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
. 最重要的是,编辑package.json
以提供有用的scripts
非常有用,例如,您可以运行npm test
。 It's common 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
. 然后npm test
将运行您的node_modules
的特定mocha
。
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. 如果是dev依赖项,则每个项目都可以使用特定于该项目的版本,并且该项目可以以受控方式迁移到较新的版本。
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 command. 因为您不需要将mocha作为命令运行。 You can run it from node_modules
like so: ./node_modules/.bin/mocha
. 您可以像这样从node_modules
运行它: ./node_modules/.bin/mocha
。
Npm has special support for this. Npm为此提供了特殊支持。 If you have the following in package.json: 如果package.json中包含以下内容:
"scripts": {
"test": "mocha"
},
"devDependencies": {
"mocha": "*"
}
Then you can execute npm test
even if you don't have mocha globally installed. 然后,即使您未全局安装摩卡,也可以执行npm test
。
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. 首先,如果您与其他开发人员合作,这是一件很不错的事情-他们不需要做任何事情,只需npm install
即可设置开发环境。
Second, and I think more useful, is this makes it easy to integrate your project with other tools like Travis etc. 其次,我认为更有用的是,这使您可以轻松地将项目与Travis等其他工具集成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.