0

I am following this tutorial. I am changing existing AWS Lambda based app to NodeJS Express app.

server.js imports the module and executes it passing the Express app as an argument:

require('./handlers/getStatus')(app);

The module is trivial, it exports a function with single argument

exports = app => {
    app.get('/v1/status', (req, res) => {
        console.log("handler starts");
        const response = {
            api: '1.0',
            status: 'OK'
        };
        return res.send(response);
    });
};

When I run this app with Node v13.11.0, there is an error:

node server.js
C:\dev\mezinamiridici\infrastructure\src\server.js:5
require('./handlers/getStatus')(app);                               ^
TypeError: require(...) is not a function
at Object.<anonymous> (C:\dev\mezinamiridici\infrastructure\src\server.js:5:32)
at Module._compile (internal/modules/cjs/loader.js:1147:30)

Why? Thank you

Leos Literak
  • 8,805
  • 19
  • 81
  • 156

1 Answers1

1

use

module.exports = app => {
    app.get('/v1/status', (req, res) => {
        console.log("handler starts");
        const response = {
            api: '1.0',
            status: 'OK'
        };
        return res.send(response);
    });
};

you will similar answer here NodeJs : TypeError: require(...) is not a function

Rahul Sharma
  • 1,393
  • 10
  • 19
  • Hmm, I tried it originally and I had an error that module has no property exports. But it works now. WebStorm stil complains this way – Leos Literak Mar 16 '20 at 19:54