node.js - cannot find module express, how to install it to make it globally available? -
i want experiment node.js stuff , installed yesterday following someone's instructions on web got , running, , got standard hello world web page on screen.
i went move onto example, in order not clutter home directory, created directory off of (~/node) , created files needed in there. low , behold, when came time run service, got no joy stating express
module couldn't found.
the instructions told me install express using -g
flag, didn't help. ran again without luck.
now i've found this: cannot find module `express` | socket.io [node.js]
and appears have install again under current directory. have done , works. case has installed under each directory want services running from? seems unnecessary duplication.
edit:
not knowing js thought go digging , found
app.use(express['static'](__dirname ));
and have realised cause of problem. further research has found this: http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders , if install once in higher level directory, should 'solve' problem. i'm not going bother uninstalling module, next project try , see how goes.
i don't know why original author suggested -g
flag when installing express, since hasn't seemed work me.
npm nice tool, allowing install node.js modules locally , globally.
local module installation
if want use module project, have install locally. that's why npm creates subdirectory called node_modules inside project directory. if use same module 2 different projects, npm download module , install twice. that's normal, helps manage different versions of same dependency.
the best way manage dependencies , install modules specific project fill package.json dependencies , install them using
npm install
inside project directory.
to access modules in code, use require() function. example, expressjs :
var express = require('express'); var app = express(); ...
global module installation
npm allows install modules globally well. remember installing module globally provides more commands in terminal, expressjs , express(1).
in order install expressjs globally, run in terminal
npm install -g express
if want use globally installed module in specific project, have install locally (in project directory, without -g).
i hope answers question.
Comments
Post a Comment