JavaScript (Node.js) : app.set('port', port); ^ TypeError: app.set is not a function

This is the error code that appears when I try to run a simple program that prints the common “Hello World”.

 app.set('port', port);
        ^

    TypeError: app.set is not a function
        at Object.<anonymous> (/home/xpuser/Desktop/Nodetest/bin/www:16:5)
        at Module._compile (module.js:409:26)
        at Object.Module._extensions..js (module.js:416:10)
        at Module.load (module.js:343:32)
        at Function.Module._load (module.js:300:12)
        at Function.Module.runMain (module.js:441:10)
        at startup (node.js:139:18)
        at node.js:968:3

Writing “module.exports = app;” Here is the error that appears:

module.exports = app;
                  ^

ReferenceError: app is not defined
     at Object. <anonymous> (/home/xpuser/Desktop/Nodetest/app.js:23:18)
     at Module._compile (module.js: 409: 26)
     at Object.Module._extensions..js (module.js: 416: 10)
     at Module.Load (module.js: 343: 32)
     at Function.Module._load (module.js: 300: 12)
     at Module.require (module.js: 353: 17)
     at require (internal / module.js: 12: 17)
     at Object. <anonymous> (/ home / xpuser / Desktop / Nodetest / bin / www: 7: 11)
     at Module._compile (module.js: 409: 26)
     at Object.Module._extensions..js (module.js: 416: 10)

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I think you’re trying to use the “Express” module, so you need this on your file:

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

Then you can set:

var port = "1000";
app.set('port', port);

module.exports = app;

Solution 2

If you have app = require(“../app”), then just make sure at the bottom of app.js there is:
module.exports = app
that way when you require it in /bin/www, it actually grabs the code.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply