Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 267
Tamil Avatar asked May 16 '12 14:05

Tamil


People also ask

What is dependency in package?

A dependency is another package that your package needs in order to work. Dependencies are specified in your pubspec. You list only immediate dependencies—the software that your package uses directly.

What is dependencies in NPM package?

"dependencies" : Packages required by your application in production. "devDependencies" : Packages that are only needed for local development and testing.

What is the meaning of dependencies in node JS?

These are the libraries you need when you run your code. For example, To run a react project, you need react-dom. In our production dependencies, you will find the express package we just installed.

What is difference between dependencies and devDependencies in package json?

The difference between these two, is that devDependencies are modules which are only required during development, while dependencies are modules which are also required at runtime. To save a dependency as a devDependency on installation we need to do an npm install --save-dev , instead of just an npm install --save.


2 Answers

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 object.

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. Note that 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 entry should be removed from the dependencies object.

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

like image 81
jmar777 Avatar answered Oct 20 '22 21:10

jmar777


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"
  }
}
like image 44
vikky singh Avatar answered Oct 20 '22 20:10

vikky singh