NPM basics with nave and Node.js
Problem
I recently installed node.js and was told that express was the way to go for routing and getting set up with web application development.
I installed the lastest version of node which apparently is incompatible with the latest express.
I looked up and found nave... Like RVM, nave allows you to switch versions of node. So I ran nave.sh install 0.4.11... That worked successfully and I was able to run.
npm install express -g.
This I thought, should install express globally.
So I run express testapp
which creates
create : testapp create : testapp/package.json create : testapp/app.js create : testapp/public/stylesheets create : testapp/public/stylesheets/style.css create : testapp/public/images create : testapp/public/javascripts create : testapp/views create : testapp/views/layout.jade create : testapp/views/index.jade
then i cd testapp/ node app.js
I get Error: Cannot find module 'express'
Is this usual behavior?
Since express is in packages.json, if i run npm install -d, it will create a node_modules directory in my application and not just symlink to the node_modules in my node path.
Solution
In a word, yes, this is the usual behavior.
When you install packages using NPM with -g option, it installs it globally, which does nice things like putting executeables on your path (i.e. the express script you used)
However, it does NOT put those packages anywhere that node can find them.
To install it so node can find the package, you must also do
cd "your express app"
npm install express
which installs locally (to the node_modules folder in the root of your application dir).
This is primarily to avoid any dependencies conflicts, and though it may seem silly, it is in fact really useful.
If you have some real reason to want to use your global install (say for example you have many applications that you want to make sure always share the same version) you can use the npm link command.
For a good rundown of NPM and global vs local see this blog post.
Discussion
View additional discussion.