What is dependency in package.json - nodejs

2019-03-27 20:56发布

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

1条回答
贪生不怕死
2楼-- · 2019-03-27 21:06

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.

查看更多
登录 后发表回答