question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

app.use is not a function

See original GitHub issue

Hello there, i’m trying to use express-ws together with already existed express application, but running into some issues.

Basic info: Node.js - v6.9.1 Express - 4.15.0 Express-ws - 3.0.0

Using express with Gulp throw nodemon:

gulp.task('express:dev', function(cb) {
        var started = false;
        return nodemon({
            script: 'server/app.js',
            watch: ['server/app.js']
        }).on('start', function() {
            // to avoid nodemon being started multiple times
            // thanks @matthisk
            if (!started) {
                cb();
                started = true;
            }
        }).on('restart', function(ev) {
                    gulpUtil.log('[nodemon] restarted');
                    gulpUtil.log('files changed:\n' + ev);
                    setTimeout(function() {
                        browserSync.notify('reloading now ...');
                        browserSync.reload({stream: false});
                    }, 1000);
                })
                .on('crash', function() {
                    gulpUtil.log('[nodemon] crashed: script crashed for some reason');
                })
                .on('exit', function() {
                    gulpUtil.log('[nodemon] exited cleanly');
                });
    });

app.js:

var NODE_PORT = 3000;
var express = require('express');
var expressWs = require('express-ws');
var logger = require('./config/logger');
// Setup server
var app = express();
app.server = require('http').createServer(app);
var expressWsObj = expressWs(express, app.server);

require('./config/express')(app);
require('./routes')(app);

app.ws('/', function(ws, req) {
  ws.on('message', function(msg) {
    console.log(msg);
  });
  console.log('socket', req.testing);
});

// Start express server
app.server.listen(NODE_PORT, 'localhost', function() {
    'use strict';
    logger.verbose('Express server serves the fake backend and listens on port', NODE_PORT);
});

// Expose app
module.exports = app;

Ends up with next error: error: uncaughtException: app.use is not a function date=Thu Mar 02 2017 09:33:57 GMT+0100 (Romance Standard Time), pid=10052, uid=null, gid=null, cwd=src\main\webapp, execPath=C:\Program Files\nodejs\node.exe, version=v6.9.1, argv=[C:\Program Files\nodejs\node.exe, src\main\webapp\server\app.js], rss=33292288, heapTotal=21008384, heapUsed=13176632, loadavg=[0, 0, 0], uptime=1793.9372317, trace=[column=9, file=src\main\webapp\server\config\express.js, function=module.exports, line=12, method=exports, native=false, column=28, file=src\main\webapp\server\app.js, function=, line=20, method=null, native=false, column=32, file=module.js, function=Module._compile, line=570, method=_compile, native=false, column=10, file=module.js, function=Object.Module._extensions…js, line=579, method=Module._extensions…js, native=false, column=32, file=module.js, function=Module.load, line=487, method=load, native=false, column=12, file=module.js, function=tryModuleLoad, line=446, method=null, native=false, column=3, file=module.js, function=Function.Module._load, line=438, method=Module._load, native=false, column=10, file=module.js, function=Module.runMain, line=604, method=runMain, native=false, column=7, file=bootstrap_node.js, function=run, line=394, method=null, native=false, column=9, file=bootstrap_node.js, function=startup, line=149, method=null, native=false], stack=[TypeError: app.use is not a function, at module.exports (src\main\webapp\server\config\express.js:12:9), at Object.<anonymous> (src\main\webapp\server\app.js:20:28), at Module._compile (module.js:570:32), at Object.Module._extensions…js (module.js:579:10), at Module.load (module.js:487:32), at tryModuleLoad (module.js:446:12), at Function.Module._load (module.js:438:3), at Module.runMain (module.js:604:10), at run (bootstrap_node.js:394:7), at startup (bootstrap_node.js:149:9)]

Any idea why express use function are not visible anymore ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
joepie91commented, Mar 2, 2017

The error seems to be occurring on line 12 in ./config/express. Could you include that file as well? Nothing seems immediately wrong in the code you’ve pasted, so I’d expect the issue to be there (since express-ws doesn’t do anything with app.use).

0reactions
ewwravicommented, Oct 14, 2022

var expressWsObj = expressWs(app, app.server);

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJS : TypeError: app.use is not a function - Stack Overflow
Just replace the first two line using the below code: var connect = require('connect'); var http = require('http'); var app = connect();.
Read more >
Console says app.use is not a function #1 - GitHub
Using Vue3 with VueCLI. Followed all the steps of installation, and ran into an issue when I get to this: const app =...
Read more >
Express/Node introduction - Learn web development | MDN
Routes are specified in the same way for both cases, though the route is optional when calling app.use() . The example below shows...
Read more >
Custom created Middleware not functioning - Node
Both log and auth Middleware are not functioning as expected. Running the program using log produces a TypeError: app.use() requires a ...
Read more >
Express Middleware - Rithm School
To include middleware we use the app.use function. Here are two examples: ... the next middleware function. If not the request will be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found