开发者

NPM basics with nave and Node.js

I recently installed node.js and was told that express was the way to go for rout开发者_StackOverflowing and getting set up with web application development.

I installed the latest 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.


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.


If you are on Windows, add location to your path.

export NODE_PATH="C:\Users\IMarek\AppData\Roaming\npm\node_modules"

Change: IMarek to your user name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜