0

Server is Ubuntu 12.0.4 x64, Node.js version is v0.8.9 (built from source code). I have succesfully installed express by sudo npm install -g express then tried below sample code to verify express but get error saying "Can not find module 'express'"

var express = require('express');
1

1 Answer 1

2

You've installed the module globally. To use it in your project you need to install it locally. From your project directory:

npm install express

Or, add it to the dependencies in your package.json file and just run npm install. This will create a node_modules directory in your project, into which all local modules will be installed.

Sign up to request clarification or add additional context in comments.

3 Comments

Could I know why this module should be installed locally rahter than globally as others?
Some modules can be installed both globally and locally. With Express installed globally you can run express in your terminal and it will initialize an Express project. When it's installed locally, you can use Express in that project.
It just depends on how you set $NODE_PATH, I can require globally installed modules just fine because the global node_modules (which, in my case, is located in /usr/local/lib) is in $NODE_PATH.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.