1
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

const port = process.env.PORT || 3000;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true,
}));

app.use('/', require('../routes')(express));

exports.server = app.listen(port, () => {
  console.log('Server active on', port);
});

Am I missing a npm package?

Error

TypeError: require(...) is not a function
at Object.<anonymous> (C:\xampp\htdocs\url-shortener\src\server.js:12:34
  at Module._compile (module.js:571:32)
  at Object.Module._extensions..js (module.js:580:10)
  at Module.load (module.js:488:32)
  at tryModuleLoad (module.js:447:12)
  at Function.Module._load (module.js:439:3)
  at Module.runMain (module.js:605:10)
  at run (bootstrap_node.js:418:7)
  at startup (bootstrap_node.js:139:9)
  at bootstrap_node.js:533:3
Community
  • 1
  • 1
Scary
  • 91
  • 3
  • 3
  • 8

1 Answers1

0

Your ../routes source file is not exporting a function.

Try replacing this line:

app.use('/', require('../routes')(express));

with:

console.log( require('../routes') );

I bet it prints an object ({ ... }) rather than a function.


Inside your ../routes file you need to explicitly export the important function:

module.exports = importantFunction;

function importantFunction(string) {
  console.log(string);
}

Now you scripts which import it can call it.

require('../routes')("hi") // should log "hi"
theonlygusti
  • 11,032
  • 11
  • 64
  • 119