简体   繁体   English

package.json 中的依赖是什么 - nodejs

[英]What is dependency in package.json - nodejs

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.在我的节点项目中,我将独立模块构建到文件夹中,以 main.js 作为入口点,并在与不同文件相同的文件夹中找到该模块的帮助程序。

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

Hence node will resolve all my helpers' dependency for modules [Ex: Aggregator] from local node_modules folder.因此,node 将从本地node_modules文件夹中解析所有我的助手对模块 [Ex: Aggregator] 的依赖。 Reason for above structure is, I don't need to care about the path on require上述结构的原因是,我不需要关心require上的路径

I use package.json to specify that entry point is main.js incase require is for Aggregator我使用 package.json 来指定入口点是 main.js incase require是 Aggregator

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

Ex: package.json of Aggregator module例如:聚合器模块的 package.json

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

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.即使我在没有任何警告的情况下将 redis 的版​​本指定为 10000,我的代码似乎也能正常工作。 I tried deleting my redis module out of project to test whether node picks it up and resolves the dependency but it didn't.我尝试从项目中删除我的 redis 模块以测试节点是否选择它并解决依赖关系,但它没有。 When to use that dependency attribute in package.json?何时在 package.json 中使用该依赖属性? Is it just a note for future reference?它只是一个笔记以备将来参考吗?

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

The dependencies value is used to specify any other modules that a given module (represented by the package.json ) requires to work. dependencies值用于指定给定模块(由package.json表示)工作所需的任何其他模块。 When you run npm install from the root folder of a given module, it will install any modules listed in that dependencies hash.当您从给定模块的根文件夹运行npm install ,它将安装该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.如果您在redis: 10000没有遇到任何错误,我的猜测是您从未运行过npm 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.反过来,如果您的代码在没有运行npm install情况下运行良好,则很可能您的代码一开始甚至不需要 redis,并且应该从dependencies哈希中删除该项目。

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.虽然不是package.json中的每个条目对于理解日常开发都是必不可少的,但要了解dependencies绝对是重要的。 I would recommend reading through the dependencies section on the npm website .我建议通读npm 网站上的依赖项部分

Dependencies are nothing but it is a third party package or we can say that modules installed using npm.依赖只是一个第三方包,或者我们可以说是使用 npm 安装的模块。 EX.前任。

{
      "name": "abc",
       "version": "1.0.0",
      "description": "",
       "main": "app.js",
     "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "npm",
    "npm@latest",
    "-gnpm",
    "npm@latest",
    "-gnpm",
    "npm@latest",
    "-g"
  ],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",   //THIS IS THIRD PARTY PACKAGE/MODULES 
    "jade": "^1.11.0",
    "nano": "^8.2.2"
  }
}

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

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