我正在构建一个完整的堆栈应用程序,经过大量的浪费,我来到这里向你们询问以下错误。
另外,我确实知道这个错误意味着什么,因为我已经读过它,但我仍然无法弄清楚某处存在的缺陷。
请求你帮助我,以便我可以继续我的工作。
文件夹架构:
nearbywi-fi(主要目录): bin,node_modules,public,server,app.js,bower.js on,package.json,procfile,yarn-error.log,package-lock.json
公开:引导程序,图片,javascripts,样式表
服务器:控制器,路线,视图
错误:
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"
}
}
答案 0 :(得分:0)
单独使用nodemon
将尝试运行index.js
在您的情况下,此文件是一个路由,而不是主应用程序,它甚至不在根目录中。所以你想运行nodemon app.js
这几乎总结在日志和错误中:
[nodemon] starting `node index.js`
Error: Cannot find module '/home/pinkman/nearbywi-fi/index.js'
根据您提供的package.json,入口点实际上应该是"./bin/www"
,因此您需要"nodemon ./bin/www"
代替。
另外,在你的package.json中,它显示"main": "index.js",
,但这应该是"main": "bin/www",