NPM basics with nave and Node.js
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
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.
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.