简体   繁体   中英

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.

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. Reason for above structure is, I don't need to care about the path on require

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

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"
    }

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 version 1.1.0-beta-4 ; node version 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. When you run npm install from the root folder of a given module, it will install any modules listed in that dependencies hash.

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.

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 .

Dependencies are nothing but it is a third party package or we can say that modules installed using 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"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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