Question

I want to experiment with some node.js stuff and I installed it yesterday following someone's instructions on the web which got it up and running, and I got the standard Hello World web page up on the screen.

I now went to move onto another example, but in order to not clutter my home directory, I created a directory off of it (~/node) and created the files I needed in there. Low and behold, when it came time to run the service, I got no joy stating the express module couldn't be found.

The instructions told me to install express using the -g flag, but that didn't help. I even ran it again without any luck.

Now I've found this: Cannot find module `express` | socket.io [node.js]

and it appears I have to install it again under the current directory. I have done that and it works. Is it the case it has to be installed under each directory that I want services running from? It seems an unnecessary duplication.

edit:

Not knowing much about js I thought I would go digging and found

app.use(express['static'](__dirname ));

and have realised this is probably the cause of my problem. Further research has found this: http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders and if I install it once in a higher level directory, that should 'solve' my problem. I'm not going to bother about uninstalling the module, but for my next project I will try it and see how it goes.

I don't know why the original author suggested the -g flag when installing express, since it hasn't seemed to work for me.

Was it helpful?

Solution

NPM is a really nice tool, allowing you to install node.js modules locally and globally.

Local module installation

If you want to use a module for your project, you have to install it locally. That's why npm creates a subdirectory called node_modules inside your project directory. If you use the same module for two different projects, npm will download the module and install it twice. That's perfectly normal, it helps you manage different versions of the same dependency.

The best way to manage dependencies and install modules for a specific project is to fill the package.json with your dependencies and install them using

npm install

inside your project directory.

To access your modules in your code, use the require() function. For example, with expressjs :

var express = require('express');
var app = express();
...

Global module installation

npm allows you to install modules globally as well. But remember that installing a module globally only provides more commands in your terminal, as with expressjs and express(1).

In order to install expressjs globally, run this in your terminal

npm install -g express

If you want to use a globally installed module in a specific project, you also have to install it locally (in your project directory, without -g).

I hope this answers clearly your question.

OTHER TIPS

Express is capable of generating a simple app structure when installed globally. See this link and scroll to Using express(1) to generate an app section. It's a good way to get you started easily.

Take a look into package.json, package.json in nodejitsu

All npm packages contain a file, usually in the project root, called package.json - this file holds various metadata relevant to the project. This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies

a package.json example:

{
    "name": "CRUD",
    "description": "A simple CRUD",
    "author": "Only for learn",
    "dependencies": {
        "express": "*",
    },
}

so for install the dependencies go to level that package.json is, and run npm install this one will install all the dependencies you need for the project.

EDIT a package.json interactive guide

I have found that when setting up node.js projects and dependencies, using Grunt [http://gruntjs.com/] has a lot of advantages. Although there are lots of different ways to setup a node and express project there is a lot to be said for using the Douglas Crockford approach and 'going with the grain'. In this case Grunt is the grain as it is becoming the de-facto standard for setting up a node project and there are existing templates for the most common types of node.js projects. You can find Grunt Express here [https://github.com/blai/grunt-express].

In this case Grunt would provide you with a project structure consistent with others, setup dependencies file for the node package manager and auto generate the express project for you. Packages are kept in a node_modules directory. If you are familiar with maven you might recognize the 'convention over configuration approach'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top