简体   繁体   English

package.json generation / npm unused packages

[英]package.json generation / npm unused packages

I'm introducing unit testing in my project and for this, I need to make myself a package.json file. 我正在我的项目中引入单元测试,为此,我需要自己制作一个package.json文件。

First question is, which unit testing suite are you using? 第一个问题是,您使用的是哪个unit testing suite I'm looking forward mocha which seem to be pretty much standard for Node.js projects. 我期待mocha似乎是Node.js项目的标准。

Second question is: Is there any magical way of generating a package.json file? 第二个问题是:是否有任何生成package.json文件的神奇方法? (for dependencies and versions) (对于依赖项和版本)

Third question is: 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. 第三个问题是:在开发我的项目时我一直在测试很多npm packages ,现在我遇到了很多可能未使用的包。 Is there any way to tell which one are useless? 有什么办法可以说出哪一个没用吗? (I saw npm list installed which is useful though) (我看到npm list installednpm list installed虽然很有用)

  1. I am using Mocha. 我正在使用摩卡。

  2. npm init

  3. npm ls will list "extraneous" next to ones that are not in your package.json. npm ls会在package.json中没有列出“无关”的旁边。 But, it sounds like you don't have a package.json yet. 但是,听起来你还没有package.json。


Basically, your workflow is very backward. 基本上,您的工作流程非常落后。 Here is how it is intended to work: 以下是它的工作方式:

  1. Start a new project with npm init . 使用npm init启动一个新项目。 It has no dependencies. 它没有依赖关系。
  2. Oh, I want to start using a package, say express ? 哦,我想开始使用包,说express Add it to package.json under dependencies, then run npm install . 将其添加到依赖项下的package.json ,然后运行npm install
  3. Oh, I want to start using a package for development, say mocha ? 哦,我想开始使用开发包,比如mocha Add it to package.json under devDependencies , then run npm install . 将它添加到package.jsondevDependencies ,然后运行npm install

You seem to have some existing code with manually-installed packages (via npm install <packageName> ), which is a mess. 你似乎有一些现有的代码与手动安装的包(通过npm install <packageName> ),这是一个烂摊子。 I suggest starting over and following the above workflow. 我建议重新开始并遵循上述工作流程。

To answer the third question : 回答第三个问题

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个不同的问题。

  1. I am also using Mocha. 我也在使用摩卡。 It has code coverage, BDD, TDD, runs in browser. 它具有代码覆盖率,BDD,TDD,在浏览器中运行。 It is pretty complete and also heavily maintained by I think one of the most brilliant javascript/node.js programmers named TJ. 这是非常完整的,并且我认为是最杰出的javascript / node.js程序员之一,名为TJ。

  2. It is almost impossible to guess which version(s) to use. 几乎不可能猜出要使用哪个版本。 Because npm does not know which version breaks which dependencies. 因为npm不知道哪个版本打破了哪个依赖项。 You could probably install all dependencies using something like node-detective . 您可以使用node-detective之类的东西安装所有依赖 Then you can just install them using npm.js from within javascript. 然后你可以在javascript中使用npm.js安装它们。 Maybe I would like to tackle this in the future. 也许我想在将来解决这个问题。

  3. I would also probably delete all dependencies , next install needed dependencies back using step(2). 我也可能会删除所有依赖项,然后使用步骤(2)重新安装所需的依赖项。 But also disc-space is not such a big case anymore with the current HDs. 但是目前的HD还不再是光盘空间的大案例。

PS: I think I also agree with Domenic PS:我想我也同意Domenic

  1. 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. 我发现节点中的单元测试通常具有挑战性,因为对dbs等的异步回调,并且主要是测试顶级功能。

  2. Here's your magic: Managing Node.js Dependencies with Shrinkwrap . 这是你的魔力: 使用Shrinkwrap管理Node.js依赖关系

  3. 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. 我的建议是积极地删除软件包,然后重新测试所有功能 - 如果它坏了,你就会知道你需要重新安装一个软件包。

Answering your third question , you can use Sweeper to list unused dependencies, and them remove them from your package.json. 回答第三个问题 ,您可以使用Sweeper列出未使用的依赖项,然后将它们从package.json中删除。 Just npm install -g sweeper then on your project directory call sweeper on the command line. 只需npm install -g sweeper然后在你的项目目录中调用命令行上的sweeper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM