I am building a full stack app and after a lot of waste efforts I came here to ask you guys about the following error.
Plus, I do know what this error means as I have read about it but still I am unable to figure out the flaw that is present in somewhere.
Request you to help me out so that I can continue my work at pace.
Folder Architecture:
nearbywi-fi(Main Directory): bin, node_modules, public, server, app.js, bower.js on, package.json, procfile, yarn-error.log, package-lock.json
Public: bootstrap, images, javascripts, stylesheets
server: controllers, routes, views
ERROR:
pinkman@pinkman:~/nearbywi-fi$ nodemon
[nodemon] 1.12.5
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node index.js`
module.js:544
throw err;
^
Error: Cannot find module '/home/pinkman/nearbywi-fi/index.js'
at Function.Module._resolveFilename (module.js:542:15)
at Function.Module._load (module.js:472:25)
at Function.Module.runMain (module.js:682:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:613:3
[nodemon] app crashed - waiting for file changes before starting...
index.js
var express = require('express');
var router = express.Router();
var ctrlLocations = require('./controllers/locations');
var ctrlOthers = require('./controllers/others');
router.get('/', ctrlLocations.homelist);
router.get('/location', ctrlLocations.locationInfo);
router.get('/location/review/new', ctrlLocations.addReview);
router.get('/about', ctrlOthers.about);
module.exports = router;
app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./server/routes/index');
var users = require('./server/routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'server', 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
package.json
{
"name": "Nearbywi-fi",
"version": "v1.3.2",
"private": true,
"scripts": {
"start": "node ./bin/www",
"postinstall": "node -e \"try { require('fs').symlinkSync(require('path').resolve('node_modules/@bower_components'), 'bower_components', 'junction') } catch (e) { }\""},
"dependencies": {
"body-parser": "~1.18.2",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.15.5",
"jade": "^1.11.0",
"jquery": "^3.2.1",
"jstransformer": "^1.0.0",
"morgan": "~1.9.0",
"nodemon": "^1.12.5",
"pug": "^2.0.0-rc.4",
"serve-favicon": "~2.4.5"
},
"main": "index.js",
"author": "vivek Gupta",
"license": "MIT",
"devDependencies": {
"bootstrap": "^4.0.0-alpha.6"
},
"engines": {
"yarn": ">= 1.0.0"
}
}
Using nodemon
by itself will try and run index.js
In your case, this file is a route, not the main application, and it's not even in the root directory. So you want to run nodemon app.js
This is pretty much summed up in the log and your error:
[nodemon] starting `node index.js`
Error: Cannot find module '/home/pinkman/nearbywi-fi/index.js'
Based on the package.json that you've provided, it seems the entry point should actually be "./bin/www"
, so you'll want "nodemon ./bin/www"
instead.
Also, in your package.json, it says "main": "index.js",
but this should be "main": "bin/www",