我正在尝试在node.js
上部署我的Heroku
应用,当我打开它时,它会给我一个internal server error
。当我查看日志时,t会说Cannot find module optmizer
。我在项目中不使用任何称为optmizer的东西。在本地主机上运行它时,它可以完美运行,但不知道为什么要询问缺少的模块。我也尝试安装optmizer module
,但似乎什么也没做
app.js
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const logger = require('morgan');
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const flash = require('connect-flash');
const session = require('express-session');
const app = express();
app.use(flash());
app.use(session({
cookie: { maxAge: 6000 },
secret: 'futuresonic',
resave: false,
saveUninitialized: false
}));
app.get('/', (req, res) => {
res.render('index', { title: 'MyWebApp', message: req.flash('success'), sessionFlash: res.locals.sessionFlash});
});
app.get('/done', (req, res) => {
res.render('done');
});
app.use((req, res, next) => {
res.locals.success = req.flash('success');
res.locals.info = req.flash('info');
res.locals.errors = req.flash('error');
res.locals.user = req.user || null;
next();
});
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/js', express.static(__dirname + '/node_modules/bootstrap/dist/js'));
app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css'));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use((req, res, next) => {
next(createError(404))
});
app.use((err, req, res, next) => {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
package.json
{
"name": "MyWebApp",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "^1.18.3",
"bootstrap": "^4.3.1",
"connect-flash": "^0.1.1",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.16.0",
"express-session": "^1.15.6",
"express-validator": "^5.3.1",
"firebase": "^5.8.6",
"firebase-admin": "^7.0.0",
"flash": "^1.1.0",
"http-errors": "~1.6.2",
"morgan": "~1.9.0",
"nodemon": "^1.18.10",
"pug": "^2.0.3"
},
"devDependencies": {
"@types/express": "^4.16.1",
"@types/node": "^11.11.3"
}
}
Heroku日志
2019-03-15T11:35:26.247394+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2019-03-15T11:35:26.247433+00:00 app[web.1]: designed for a production environment, as it will leak
2019-03-15T11:35:26.247436+00:00 app[web.1]: memory, and will not scale past a single process.
2019-03-15T11:35:26.981657+00:00 heroku[web.1]: State changed from starting to up
2019-03-15T11:35:28.691683+00:00 heroku[router]: at=info method=GET path="/" host=pure-hamlet-86611.herokuapp.com request_id=1bcb11de-4b03-44fd-9835-7b48f0e2bf4b fwd="109.69.85.106" dyno=web.1 connect=1ms service=129ms status=500 bytes=569 protocol=https
2019-03-15T11:35:28.690100+00:00 app[web.1]: Error: Cannot find module './optimizer/level-0/optimize'
2019-03-15T11:35:28.690112+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
2019-03-15T11:35:28.690115+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:508:25)
2019-03-15T11:35:28.690117+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:637:17)
2019-03-15T11:35:28.690118+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:22:18)
2019-03-15T11:35:28.690120+00:00 app[web.1]: at Object.<anonymous> (/app/node_modules/clean-css/lib/clean.js:8:22)
2019-03-15T11:35:28.690122+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:701:30)
2019-03-15T11:35:28.690124+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
2019-03-15T11:35:28.690125+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:600:32)
2019-03-15T11:35:28.690127+00:00 app[web.1]: at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
2019-03-15T11:35:28.690129+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:531:3)
答案 0 :(得分:0)
在将我的pug版本更新为2.0.3并运行npm audit
后,我收到此错误消息。不知道为什么。要修复,我将package.json哈巴狗版本更改为2.0.0,删除了package-lock.json,然后使用npm install
重新创建了它。