Node.js Express: TypeError: object is not a function
NickName:mitmath514 Ask DateTime:2019-01-02T03:54:08

Node.js Express: TypeError: object is not a function

When I run the following code:

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

app.get('/', function(req, res){
    res.send('<h1>Hello world</h1>');
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});

I get the following error:

/var/www/html/test.me/public_html/giftcard/index.js:1
rts, require, module, __filename, __dirname) { var app = require('express')();
                                                                           ^
TypeError: object is not a function
    at Object.<anonymous> (/var/www/html/test.me/public_html/giftcard/index.js:1:91)
    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

What does this error mean? Is the path to express incorrect? I have one directory with packages.json, index.js (the file above), and node_modules, and I have express installed. Where is the error coming from?

Copyright Notice:Content Author:「mitmath514」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53998493/node-js-express-typeerror-object-is-not-a-function

Answers
quirimmo 2019-01-01T20:11:06

The error should be at this line: \n\nvar http = require('http').Server(app);\n\nFor what you are trying to do, you simply need express without http\n\nconst app = require('express')();\n\napp.get('/', function(req, res){\n res.send('<h1>Hello world</h1>');\n});\n\napp.listen(3000, () => console.log('listening on 3000'));\n\n\nIf you want to use also http, try using this syntax:\n\nconst app = require('express')();\nconst server = require('http').createServer(app);\nserver.listen(3000);\n",


gaaaaaa 2019-01-01T20:01:53

Why are you using this:\n\nvar app = require('express')();\nvar http = require('http').Server(app);` \n\n\nwhile you can do like this :\n\nvar app = new require('express')();\n\napp.get('/', function(req, res){\n res.send('<h1>Hello world</h1>');\n});\n\napp.listen(3000, function(){\n console.log('listening on *:3000');\n});\n\n\nHowever, your code works perfectly on my node.js .\n\nvar app = new require('express')();\nvar http = require('http').Server(app);\n\napp.get('/', function(req, res){\n res.send('<h1>Hello world</h1>');\n});\n\nhttp.listen(3000, function() {\n console.log('listening on *:3000')\n});\n",


More about “Node.js Express: TypeError: object is not a function” related questions

Node.js Express: TypeError: object is not a function

When I run the following code: var app = require('express')(); var http = require('http').Server(app); app.get('/', function(req, res){ res.send('&lt;h1&gt;Hello world&lt;/h1&

Show Detail

TypeError: object is not a function - node.js / express

I get the following Type Error when I run the node.js application /Users/khinester/Sandboxes/zeitgeist/Blade/server.js:5 application = require("./.app/")(); ^

Show Detail

Node.JS Express Authentication App Creation TypeError

I'm working through a tutorial on Node.js that begins with a simple authentication program. This is the second time I'm doing this tutorial, and the first time, everything worked fine. However, upon

Show Detail

TypeError: object is not a function showing at express

Today I am learning Nodejs (Beginner) and execute CURD operation with mysql. I am working with http://teknosains.com/i/simple-crud-nodejs-mysql . Everything working fine but as last when I am runn...

Show Detail

node.js express createServer() not a function

I'm trying to build an express project. After I npm install, create server.js and node server.js, I got this error: var app = module.exports = express.createServer(); ...

Show Detail

Express 4 session TypeError: object is not a function

I am learning nodejs with Express 4 to develop a web app and following some blogs, this error is shown when trying to launch the project: [nodetest1]# npm start [email protected] start /root/

Show Detail

Node.js server.get() TypeError: undefined is not a function

var express = require('express'), http = require('http'), app = express(), server = http.createServer(app), io = require('socket.io').listen(server), //pass a http.Server instance ...

Show Detail

TypeError: string.charCodeAt is not a function. How to encode object in node.js using express (restful api)

I use restful API with express &amp; node.js and want to encode data to utf-8. I install utf8 with npm install utf8 I set const utf8 = require('utf8'); I use utf8.encode(string) to object like t...

Show Detail

Express & Node.js Exception: 500 TypeError: fn is not a function

I created a Node.js project using Express and got this exception when using a customized routes. 500 TypeError: fn is not a function at callbacks (/WallaceBot/WallaceBot/node_modules/express/...

Show Detail

Node.js: object is not a function

I have just started working with Nodejs and trying to create my first node (using express)application. I am getting an error "TypeError: object is not a function" with below message: /home/sears/

Show Detail