javascript package.json 生成 / npm 未使用的包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10109186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
package.json generation / npm unused packages
提问by Tommy B.
I'm introducing unit testing in my project and for this, I need to make myself a package.json
file.
我正在我的项目中引入单元测试,为此,我需要为自己制作一个package.json
文件。
First questionis, which unit testing suite
are you using? I'm looking forward mocha
which seem to be pretty much standard for Node.js
projects.
第一个问题是,unit testing suite
您使用的是哪个?我很期待mocha
这似乎是Node.js
项目的标准。
Second questionis: Is there any magical way of generating a package.json
file? (for dependencies and versions)
第二个问题是:有没有什么神奇的生成package.json
文件的方法?(对于依赖项和版本)
Third questionis: I've been testing a lot of npm packages
while developing my project and now I'm stuck with a lot of probably unused packages. Is there any way to tell which one are useless? (I saw npm list installed
which is useful though)
第三个问题是:我npm packages
在开发项目时进行了大量测试,现在我遇到了很多可能未使用的包。有什么办法可以判断哪个没用?(npm list installed
虽然我看到哪个有用)
采纳答案by Alfred
I am also using Mocha. It has code coverage, BDD, TDD, runs in browser. It is pretty complete and also heavily maintained by I think one of the most brilliant javascript/node.js programmers named TJ.
It is almost impossible to guess which version(s) to use. Because npm does not know which version breaks which dependencies. You could probably install all dependencies using something like node-detective. Then you can just install them using npm.js from within javascript. Maybe I would like to tackle this in the future.
I would also probably delete all dependencies , next install needed dependencies back using step(2). But also disc-space is not such a big case anymore with the current HDs.
我也在用摩卡。它具有代码覆盖率、BDD、TDD,在浏览器中运行。它非常完整,并且由我认为最出色的 javascript/node.js 程序员之一 TJ 维护。
几乎不可能猜测要使用哪个版本。因为 npm 不知道哪个版本破坏了哪个依赖项。您可能可以使用node-detective 之类的东西安装所有依赖项。然后你可以在 javascript 中使用 npm.js 安装它们。也许我想在未来解决这个问题。
我也可能会删除所有依赖项,接下来使用步骤(2)安装所需的依赖项。但是对于当前的 HD,磁盘空间也不再是一个大问题。
P.S: I think I also agree with Domenic
PS:我想我也同意Domenic
回答by Domenic
I am using Mocha.
npm init
npm ls
will list "extraneous" next to ones that are not in your package.json. But, it sounds like you don't have a package.json yet.
我正在使用摩卡咖啡。
npm init
npm ls
将在 package.json 中没有的内容旁边列出“无关的”。但是,听起来您还没有 package.json。
Basically, your workflow is very backward. Here is how it is intended to work:
基本上,您的工作流程非常落后。这是它的工作原理:
- Start a new project with
npm init
. It has no dependencies. - Oh, I want to start using a package, say
express
? Add it topackage.json
under dependencies, then runnpm install
. - Oh, I want to start using a package for development, say
mocha
? Add it topackage.json
underdevDependencies
, then runnpm install
.
- 开始一个新项目
npm init
。它没有依赖项。 - 哦,我想开始使用一个包,比如说
express
?将其添加到package.json
依赖项下,然后运行npm install
. - 哦,我想开始使用一个包进行开发,比如说
mocha
?将其添加到package.json
underdevDependencies
,然后运行npm install
。
You seem to have some existing code with manually-installed packages (via npm install <packageName>
), which is a mess. I suggest starting over and following the above workflow.
您似乎有一些带有手动安装包的现有代码(通过npm install <packageName>
),这是一团糟。我建议重新开始并遵循上述工作流程。
回答by Max Truxa
To answer the third question:
回答第三个问题:
npm prune
npm prune
will remove all installed modules that are no longer mentioned in your package.json.
将删除package.json 中不再提及的所有已安装模块。
And you should really have asked 3 separate questions.
你真的应该问 3 个不同的问题。
回答by ControlAltDel
I am using vows. It's pretty good, but not perfect. I have found unit testing in node to often be challenging because of async callbacks to dbs & such, and have mostly been testing top level functionality.
Here's your magic: Managing Node.js Dependencies with Shrinkwrap.
The only way to know what packages you are using is to know. You can't generate this programmatically. My advice would be to remove packages aggressively, then retest all functionality - if it breaks, you'll know you need to reinstall one of your packages.
我正在使用誓言。它非常好,但并不完美。我发现 node 中的单元测试通常具有挑战性,因为对 dbs 等的异步回调,并且主要测试顶级功能。
这是你的魔法:使用 Shrinkwrap 管理 Node.js 依赖项。
了解您正在使用的软件包的唯一方法是了解。您不能以编程方式生成它。我的建议是积极删除软件包,然后重新测试所有功能——如果它坏了,你就会知道你需要重新安装一个软件包。
回答by Luiz Sócrate
Answering your third question, you can use Sweeperto list unused dependencies, and them remove them from your package.json. Just npm install -g sweeper
then on your project directory call sweeper
on the command line.
回答您的第三个问题,您可以使用Sweeper列出未使用的依赖项,并将它们从您的 package.json 中删除。就在npm install -g sweeper
这时,sweeper
在命令行上调用您的项目目录。