Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a package.json file so that all dependencies are downloaded with "npm install"

Tags:

node.js

npm

I wrote a simple application using node. It depends on express, mongodb and mongoose (easy). So, I created a file called package.json and put this in it:

{
  "name": "booking-dojo",
  "description": "Booking dojo app",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "express": "3.x",
    "mongodb": "*",
    "mongoose": "*"
  }
}

I then ran npm install, expecting npm to install those modules and their dependencies. The result was disappointing:

[email protected] /home/merc/Synced/Development/Bookings/app/server
├─┬ [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ └─┬ [email protected] 
│   └── [email protected] 
├─┬ [email protected] 
│ └── [email protected] 
└─┬ [email protected] 
  ├── [email protected] 
  └── [email protected] 

I am confused by this, as I know that express needs jade (and much more), and mongoose needs mongodb.
If I go into node_modules/jade and run npm install, the result from the main tree is very different:

[email protected] /home/merc/Synced/Development/Bookings/app/server
├─┬ [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ ├─┬ [email protected] 
│ │ └─┬ [email protected] 
│ │   └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected]
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ └─┬ [email protected] 
│ │   └── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ └─┬ [email protected] 
│   └─┬ [email protected] 
│     ├── [email protected] 
│     ├── [email protected] 
│     ├── [email protected] 
│     └── [email protected] 
├─┬ [email protected] 
│ └── [email protected] 
└─┬ [email protected] 
  ├── [email protected] 
  └── [email protected] 

So, express has grown a lot. It looks like npm install is only loading some of the dependencies of the sub-modules.

Can somebody please shed some light on this? Why are some dependencies missing? Am I doing something wrong? (likely)

Thanks!

Merc.

like image 943
Merc Avatar asked Aug 26 '12 02:08

Merc


3 Answers

You are confused about at least 2 points.

First, express does not depend on jade, as you can see by reading the node_modules/express/package.json file:

  "dependencies": {
    "connect": "2.4.2",
    "commander": "0.6.1",
    "range-parser": "0.0.4",
    "mkdirp": "0.3.3",
    "cookie": "0.0.4",
    "crc": "0.2.0",
    "fresh": "0.1.0",
    "methods": "0.0.1",
    "send": "0.0.3",
    "debug": "*"
  }

Express does, however, work with jade if it is available, as well as many other template engines. So to fix this list jade as a dependency in your package.json file and you'll be fine.

Second, npm only installs node packages, not third party things like mongodb. You need to install mongodb and any other dependencies that are not npm modules using other means (apt-get, yum, manual install, etc).

So npm DOES install dependencies recursively, but only npm modules.

like image 80
Peter Lyons Avatar answered Nov 06 '22 14:11

Peter Lyons


The answer was provided by Brandon in a comment to another answer:

"Another thing to note is that if a package depends on a module that can be resolved further up in the dependency chain, it will. For example, since you have mongodb in your package.json, Mongoose doesn't need to install its own mongodb. – Brandon Tilley 2 days ago

Thank you Brandon! (And this is the answer...)

like image 24
Merc Avatar answered Nov 06 '22 14:11

Merc


use this sample

{
  "name": "app",
  "version": "0.0.1",
  "main":"test.js",
  "author":"Test",
  "description": "For test ",
  "dependencies": {
    "express": "latest",
    "mongoose": "latest"
  }

}
like image 4
M.Ganji Avatar answered Nov 06 '22 12:11

M.Ganji