a8hok
a8hok

Reputation: 308

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

Upvotes: 5

Views: 47365

Answers (7)

AD_
AD_

Reputation: 51

Run this command to verify if express is properly installed in the project directory:

npm list

This command will also show all the packages which have been installed in your project directory.

Upvotes: 1

Tejas Veer
Tejas Veer

Reputation: 533

If you successfully installed EXPRESS but then also you get ReferenceError: express is not defined.

Then,

just put name of variable in first line and function name in second line same like this:-

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

Not Like This:-

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

Upvotes: 1

Ankita ugale
Ankita ugale

Reputation: 1

Just close your terminal and start it by right-clicking on it and selecting "Run as administrator" and then follow to the directory in which you want to create your server and run npm install express done!

Upvotes: 0

Vickrant
Vickrant

Reputation: 1313

check the file permissions also please. Also may be your user login do not have the permission to make changes in the system. You may have to use sudo before the command to do so if you are on ubuntu or not a root user.

Upvotes: 0

Silom
Silom

Reputation: 746

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

Upvotes: 15

Murugan Pandian
Murugan Pandian

Reputation: 778

use this command in your terminal npm install express

make sure install inside your project folder

Upvotes: 0

jingyinggong
jingyinggong

Reputation: 646

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

Upvotes: 6

Related Questions