user2024080 user2024080 - 3 months ago 18
Node.js Question

`npm` error as `Error: Cannot find module` - how to fix

In my application, when i try to call the service I am getting the error as like this:

throw err;

Error: Cannot find module '/api/sessions'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Tutorials\try\ModernWebApp\server.js:13:9)
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
[14:38:53] [nodemon] app crashed - waiting for file changes before starting...

here is my

var express = require('express');
var bodyParser = require('body-parser');

var app = express();

app.use(express.static( __dirname + '/assets'))

app.use(express.static( __dirname + '/templates'))

//if i remove both this 2 lines, no issues come.
app.use(require('/api/sessions', require( __dirname + '/controllers/api/sessions')));
app.use(require('/api/users', require('./controllers/api/users')));

app.get('/', function( req, res ){
res.sendFile( __dirname + '/layouts/app.html');

app.listen( 8888, function(){
console.log('app listening!' + 8888 );

what is wrong here, how to solve this. the 2 files are nothing but to handling the query from front end.

for sample:

var router = require('express').Router();
var bcrypt = require('bcryptjs');
var jwt = require('jwt-simple');
var User = require('../../models/user');
var config = require('../../config');'/sessions', function( req, res, next ) {

username: req.body.username,
.exec(function( err, user ) {
if( err ) { return next( err )}

if(!user) { return res.send(401)}, user.password, function( err, valid ) {

if( err ) { return next( err )}

if(!valid) { return res.send(401)}

var token = jwt.encode({username:user.username}, config.secret);

res.send( token );




module.exports = router;


try using just.


If that doesn't work give the proper path of the module you want to require.Because require function only take one parameter that is a module ID in string.