package.json中的依赖项是什么-Node.js [英] What is dependency in package.json - nodejs

查看:155
本文介绍了package.json中的依赖项是什么-Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的节点项目中,我以main.js作为入口点在文件夹中构建独立的模块,并在与其他文件相同的文件夹中找到该模块的帮助程序.

In my node projcet I build independent modules in to folder with main.js as entry point and locate helpers for that module in the same folder as different files.

Ex:
Aggregator:
     |___package.json
     |___main.js
     |___node_modules
         |_____helper1.js
         |_____helper2.js

因此,节点将解决本地node_modules文件夹中我对模块[Ex:Aggregator]的所有依赖关系.以上结构的原因是,我不需要关心require

Hence node will resolve all my helpers' dependency for modules [Ex: Aggregator] from local node_modules folder. Reason for above structure is, I don't need to care about the path on require

require用于聚合器的情况下,我使用package.json来指定入口点是main.js

I use package.json to specify that entry point is main.js incase require is for Aggregator

Ex:
//Sample.js
require('Aggregator'); // Resolves to Aggregator/main.js

例如: 聚合器模块的package.json

Ex: package.json of Aggregator module

  {
        "name": "Aggregator"
      , "description": "Returns Aggregates"
      , "keywords": ["aggregate"]
      , "author": "Tamil"
      , "contributors": []
      , "dependencies": {
            "redis": "0.6.7"
        }
      , "lib"           : "."
      , "main"          : "./main.js"
      , "version"       : "1.0"
    }

这里的依赖项列是做什么用的?我引用了链接.即使我在没有任何警告的情况下将redis的版本指定为10000,我的代码似乎也可以正常工作.我尝试从项目中删除我的redis模块,以测试节点是否选择了它并解决了依赖性,但没有.什么时候在package.json中使用该依赖项属性?只是便条,以备将来参考吗?

Here what is the dependency column for? I referred this link. My code seems to work even if I specify version of redis as 10000 without any warning. I tried deleting my redis module out of project to test whether node picks it up and resolves the dependency but it didn't. When to use that dependency attribute in package.json? Is it just a note for future reference?

npm版本1.1.0-beta-4; 节点版本v0.6.6

npm version 1.1.0-beta-4 ; node version v0.6.6

推荐答案

dependencies值用于指定给定模块(由package.json表示)需要工作的任何其他模块.当您从给定模块的根文件夹中运行npm install时,它将安装该dependencies哈希中列出的所有模块.

The dependencies value is used to specify any other modules that a given module (represented by the package.json) requires to work. When you run npm install from the root folder of a given module, it will install any modules listed in that dependencies hash.

如果其中列出的redis: 10000没有出现任何错误,我想您永远不会运行npm install,因此它甚至从未尝试安装Redis.反过来,如果您的代码在没有运行npm install的情况下也可以正常工作,则很可能您的代码甚至根本不需要redis,因此应从dependencies哈希中删除该项目.

If you didn't get any errors with redis: 10000 listed in there, my guess is that you never ran npm install, and therefore it never even tried to install redis. In turn, if your code is working fine without having run npm install, most likely your code doesn't even need redis in the first place, and that item should be removed from the dependencies hash.

尽管不是package.json中的每个条目对于理解日常开发都是必不可少的,但dependencies绝对是重要的.我建议您仔细阅读npm网站上的依赖性部分.

While not every entry in the package.json is essential to understand for day-to-day development, dependencies is definitely important to be aware of. I would recommend reading through the dependencies section on the npm website.

这篇关于package.json中的依赖项是什么-Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆