Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

express is not defined in express

var express = require('express');
var app = express();
app.get('/', function(req, res){
    res.send('hello world');
});
app.listen(3000);

I am getting the following error.

> D:\nodejs\mynode\index.js:2  
> var app=express();
    ^
ReferenceError: express is not defined
    at Object.<anonymous> (D:\nodejs\mynode\index.js:2:9)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3
like image 944
a8hok Avatar asked Nov 29 '22 11:11

a8hok


2 Answers

For using express you need to follow these steps:

1) Basic setup with express

Maybe you have to use sudo

npm install -g express

This command will install express globally. Also you can now use express on the command line.

You can now use express to setup a basic environment using this command.

express [options] [dir]

Options:

-h, --help          output usage information
-V, --version       output the version number
-s, --sessions      add session support
-e, --ejs           add ejs engine support (defaults to jade)
-J, --jshtml        add jshtml engine support (defaults to jade)
-H, --hogan         add hogan.js engine support
-c, --css <engine>  add stylesheet <engine> support (less|stylus) (defaults to plain css)
-f, --force         force on non-empty directory

2) Basic setup with the package.json

Create two files:

package.json

index.js

The package.json includes lots of project informations.

This is a example package.json:

{
    "name": "MyProject",
    "version": "0.0.1",
    "private": true,
    "dependencies": {
        "express": "~3.4.4"
    }
}

If you join now your project folder and run npm install

npm will look up the "dependencies" and install them.

Now open you index.js and write following.

var express = require('express'); // Get the module
var app = express(); // Create express by calling the prototype in var express
like image 63
Silom Avatar answered Dec 04 '22 06:12

Silom


should enter your code directory and use "npm install" in your shell!

like image 39
jingyinggong Avatar answered Dec 04 '22 05:12

jingyinggong