My package.json looks like this (name/description/etc. omitted).
{
"dependencies": {
"express": "3.3.4",
"jade": "0.34.x",
"mongoose": "3.6.x"
},
"devDependencies": {
"vows": "0.7.x"
}
}
I used express on the repository and ran the auto-generated node app.js. This worked, but when I used curl http://localhost:port I got the error "Cannot find module character-parser." I ran npm install character-parser and then I got "Cannot find module transformers." This happened a few more times, but after I installed all of them the app started working.
I thought that npm install was supposed to install dependencies recursively. This also worries me because I obviously want the package.json to be usable when the app is deployed.
I got this exact error while I was npm installing for https://github.com/HenrikJoreteg/humanjs-sample-app/
I'm on a Windows machine, so I suspected that it was an issue with the odd restrictions that Windows has on the number of characters in a file path.
I resolved this by shorting my base path to a three character folder name in my root (in this case going from C:\projects\humanjs-sample-app to C:\hjs). When I re-ran npm install everything worked. I'm not happy with that resolution. I shouldn't have to worry about my base path name being too long. This is one of the reasons that people often don't do node development on Windows machines.
An alternate resolution is to develop on Linux or Mac, if you aren't already. That's probably my long term strategy.
When you run npm install <name-of-package> it will install the package to your node_modules folder, but will not add it as a dependency. In order to install the package and save it as a dependency in your package.json, you must use the --save flag like so:
npm install <name-of-package> --save
The npm documentation provides further information on additional flags that can be used such as the --save-dev flag for saving development dependencies and the --save-optional flag for saving optional dependencies to your package.json.
Here's a simple script to collect the dependencies in ./node_modules:
var fs = require("fs");
function main() {
fs.readdir("./node_modules", function (err, dirs) {
if (err) {
console.log(err);
return;
}
dirs.forEach(function(dir){
if (dir.indexOf(".") !== 0) {
var packageJsonFile = "./node_modules/" + dir + "/package.json";
if (fs.existsSync(packageJsonFile)) {
fs.readFile(packageJsonFile, function (err, data) {
if (err) {
console.log(err);
}
else {
var json = JSON.parse(data);
console.log('"'+json.name+'": "' + json.version + '",');
}
});
}
}
});
});
}
For one project I'm working on, the output looks like this:
"progress": "0.1.0",
"request": "2.11.4",
If you remember to remove the comma from the last entry, you can copy and paste the output.