Node.js 未在Heroku上加载快速自定义模块
请参阅下面的更新 我已经用Express编写了一个Node.js应用程序,它在本地运行得很好,但是当我在Heroku上运行该应用程序时,会出现以下错误:Node.js 未在Heroku上加载快速自定义模块,node.js,heroku,module,express,Node.js,Heroku,Module,Express,请参阅下面的更新 我已经用Express编写了一个Node.js应用程序,它在本地运行得很好,但是当我在Heroku上运行该应用程序时,会出现以下错误: 2013-01-19T21:55:42+00:00 app[web.1]: module.js:340 2013-01-19T21:55:42+00:00 app[web.1]: throw err; 2013-01-19T21:55:42+00:00 app[web.1]: ^ 2013-01-19T21:55:42+00:0
2013-01-19T21:55:42+00:00 app[web.1]: module.js:340
2013-01-19T21:55:42+00:00 app[web.1]: throw err;
2013-01-19T21:55:42+00:00 app[web.1]: ^
2013-01-19T21:55:42+00:00 app[web.1]: Error: Cannot find module './blog/blog'
2013-01-19T21:55:42+00:00 app[web.1]: at Function.Module._load (module.js:312:12)
2013-01-19T21:55:42+00:00 app[web.1]: at Module.require (module.js:362:17)
2013-01-19T21:55:42+00:00 app[web.1]: at Object.Module._extensions..js (module.js:467:10)
2013-01-19T21:55:42+00:00 app[web.1]: at require (module.js:378:17)
2013-01-19T21:55:42+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:15:12)
2013-01-19T21:55:42+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:338:15)
2013-01-19T21:55:42+00:00 app[web.1]: at Module.load (module.js:356:32)
2013-01-19T21:55:42+00:00 app[web.1]: at Module.runMain (module.js:492:10)
2013-01-19T21:55:42+00:00 app[web.1]: at Function.Module._load (module.js:280:25)
2013-01-19T21:55:42+00:00 app[web.1]: at Module._compile (module.js:449:26)
2013-01-19T21:55:43+00:00 heroku[web.1]: Process exited with status 1
2013-01-19T21:55:43+00:00 heroku[web.1]: State changed from starting to crashed
blog.js:
//requires and starts up app
var express = require('express');
var app = express();
//db setup
var mongoose = require('mongoose')
, dbURI = 'localhost/brads-projects';
//configures app for production, connects to MongoHQ databse rather than localhost
app.configure('production', function () {
dbURI = process.env.MONGOHQ_URL;
});
//requires the various project files
var blog = require('./blog/blog').blog;
//tries to connect to database.
mongoose.connect(dbURI);
//once connection to database is open, then rest of app runs
mongoose.connection.on('open', function () {
//runs the blog app
blog(app, express);
app.listen(process.env.PORT || 5000);
});
//in the event of a connection to database error, the app will not run
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
module.exports.blog = function(app, express) {
//models
var postmodel = require('./models/post').postmodel
, usermodel = require('./models/user').usermodel
, notificationmodel = require('./models/notification').notificationmodel
, commentmodel = require('./models/comment').commentmodel;
//controllers
var indexHandler = require('./controllers/index').index
, newpostHandler = require('./controllers/newpost').newpost
, postandidHandler = require('./controllers/postandid').postandid
, newPostHandler = require('./controllers/newpost').newpost
, searchHandler = require('./controllers/search').postsearch
, loginHandler = require('./controllers/login').login
, logoutHandler = require('./controllers/login').logout
, dashboardHandler = require('./controllers/dashboard').dashboard
, registerHandler = require('./controllers/register').register
, userSettingsHandler = require('./controllers/usersettings').usersettings
, editpostHandler = require('./controllers/editpost').editpost
, newCommentHandler = require('./controllers/newcomment').newcomment;
//misc requires
var MemStore = require('connect/lib/middleware/session/memory');
//configures app for general stuff needed such as bodyParser and static file directory
app.configure(function () {
app.use(express.bodyParser());
app.use(express.static(__dirname + '/static'));
app.use(express.cookieParser('lockirlornie123'));
app.use(express.session({store: MemStore( {
reapInterval: 60000 * 10
})}));
});
//requires a user session for access
function requiresLogin(request, response, next) {
if (request.session.user) {
next();
} else {
response.redirect('/blog/login');
}
};
//requires user session and admin for access
function requiresLoginAndAdmin(request, response, next) {
if (request.session.user && request.session.user.role === 'admin') {
next();
} else {
if (request.session.user) {
response.redirect('/blog');
} else {
response.redirect('/blog/login');
}
}
};
console.log("loaded");
var PostModel = new postmodel();
var Post = PostModel.setupPostSchema();
var UserModel = new usermodel();
var User = UserModel.setupUserSchema();
var NotificationModel = new notificationmodel();
var Notification = NotificationModel.setupNotificationSchema();
NotificationModel.clickNotificationHandler(app, Notification);
var CommentModel = new commentmodel();
var Comment = CommentModel.setupCommentSchema();
app.set('views', __dirname + '/views');
app.set('view engine','jade');
/*
var newuser = new User({email: "brad.ross.35@gmail.com", password: UserModel.createHashPass("Brad1234"), role: 'admin', activated: true});
newuser.save(function (err) {
if (err) {
console.log("error saving!");
} else {
console.log("successfully created!");
}
});
*/
//get request for the home page that displays the 10 most recent posts
indexHandler(app, Post, PostModel, NotificationModel.getNotifications, Notification);
//get request for the unique page for every post
postandidHandler(app, Post, NotificationModel.getNotifications, Notification, CommentModel.getComments, Comment);
//post request for the submit url that creates a new post and puts it into the database
//if a get request is sent to the sumbit page, it redirects users away from the /submit url in order to keep them away and not cause errors.
newPostHandler(app, Post, requiresLogin, PostModel, NotificationModel.getNotifications, Notification);
//post request to create a new comment
newCommentHandler(app, Comment, requiresLogin, CommentModel, NotificationModel.getNotifications, Notification, NotificationModel.createNotification, Post);
//get request for search page that both displays search results and allows users to create new search queries
searchHandler(app, Post, NotificationModel.getNotifications, Notification);
//login page get request and post request
loginHandler(app, UserModel.authenticate, User);
//logout page that redirects back to home
logoutHandler(app);
//dashboard page for managing posts by user
//and if user is an admin, adding and deleting users
dashboardHandler(app, User, Post, requiresLoginAndAdmin, NotificationModel.getNotifications, Notification, Comment);
//a page for users to register for posting priveleges
registerHandler(app, User, UserModel, NotificationModel.createNotification, Notification);
//a page for user settings
userSettingsHandler(app, User, UserModel, requiresLogin);
//a page to edit posts
editpostHandler(app, Post, requiresLogin, NotificationModel.getNotifications, Notification);
};
更新:
//requires and starts up app
var express = require('express');
var app = express();
//db setup
var mongoose = require('mongoose')
, dbURI = 'localhost/brads-projects';
//configures app for production, connects to MongoHQ databse rather than localhost
app.configure('production', function () {
dbURI = process.env.MONGOHQ_URL;
});
//requires the various project files
var blog = require('./blog/blog').blog;
//tries to connect to database.
mongoose.connect(dbURI);
//once connection to database is open, then rest of app runs
mongoose.connection.on('open', function () {
//runs the blog app
blog(app, express);
app.listen(process.env.PORT || 5000);
});
//in the event of a connection to database error, the app will not run
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
module.exports.blog = function(app, express) {
//models
var postmodel = require('./models/post').postmodel
, usermodel = require('./models/user').usermodel
, notificationmodel = require('./models/notification').notificationmodel
, commentmodel = require('./models/comment').commentmodel;
//controllers
var indexHandler = require('./controllers/index').index
, newpostHandler = require('./controllers/newpost').newpost
, postandidHandler = require('./controllers/postandid').postandid
, newPostHandler = require('./controllers/newpost').newpost
, searchHandler = require('./controllers/search').postsearch
, loginHandler = require('./controllers/login').login
, logoutHandler = require('./controllers/login').logout
, dashboardHandler = require('./controllers/dashboard').dashboard
, registerHandler = require('./controllers/register').register
, userSettingsHandler = require('./controllers/usersettings').usersettings
, editpostHandler = require('./controllers/editpost').editpost
, newCommentHandler = require('./controllers/newcomment').newcomment;
//misc requires
var MemStore = require('connect/lib/middleware/session/memory');
//configures app for general stuff needed such as bodyParser and static file directory
app.configure(function () {
app.use(express.bodyParser());
app.use(express.static(__dirname + '/static'));
app.use(express.cookieParser('lockirlornie123'));
app.use(express.session({store: MemStore( {
reapInterval: 60000 * 10
})}));
});
//requires a user session for access
function requiresLogin(request, response, next) {
if (request.session.user) {
next();
} else {
response.redirect('/blog/login');
}
};
//requires user session and admin for access
function requiresLoginAndAdmin(request, response, next) {
if (request.session.user && request.session.user.role === 'admin') {
next();
} else {
if (request.session.user) {
response.redirect('/blog');
} else {
response.redirect('/blog/login');
}
}
};
console.log("loaded");
var PostModel = new postmodel();
var Post = PostModel.setupPostSchema();
var UserModel = new usermodel();
var User = UserModel.setupUserSchema();
var NotificationModel = new notificationmodel();
var Notification = NotificationModel.setupNotificationSchema();
NotificationModel.clickNotificationHandler(app, Notification);
var CommentModel = new commentmodel();
var Comment = CommentModel.setupCommentSchema();
app.set('views', __dirname + '/views');
app.set('view engine','jade');
/*
var newuser = new User({email: "brad.ross.35@gmail.com", password: UserModel.createHashPass("Brad1234"), role: 'admin', activated: true});
newuser.save(function (err) {
if (err) {
console.log("error saving!");
} else {
console.log("successfully created!");
}
});
*/
//get request for the home page that displays the 10 most recent posts
indexHandler(app, Post, PostModel, NotificationModel.getNotifications, Notification);
//get request for the unique page for every post
postandidHandler(app, Post, NotificationModel.getNotifications, Notification, CommentModel.getComments, Comment);
//post request for the submit url that creates a new post and puts it into the database
//if a get request is sent to the sumbit page, it redirects users away from the /submit url in order to keep them away and not cause errors.
newPostHandler(app, Post, requiresLogin, PostModel, NotificationModel.getNotifications, Notification);
//post request to create a new comment
newCommentHandler(app, Comment, requiresLogin, CommentModel, NotificationModel.getNotifications, Notification, NotificationModel.createNotification, Post);
//get request for search page that both displays search results and allows users to create new search queries
searchHandler(app, Post, NotificationModel.getNotifications, Notification);
//login page get request and post request
loginHandler(app, UserModel.authenticate, User);
//logout page that redirects back to home
logoutHandler(app);
//dashboard page for managing posts by user
//and if user is an admin, adding and deleting users
dashboardHandler(app, User, Post, requiresLoginAndAdmin, NotificationModel.getNotifications, Notification, Comment);
//a page for users to register for posting priveleges
registerHandler(app, User, UserModel, NotificationModel.createNotification, Notification);
//a page for user settings
userSettingsHandler(app, User, UserModel, requiresLogin);
//a page to edit posts
editpostHandler(app, Post, requiresLogin, NotificationModel.getNotifications, Notification);
};
由于下面的建议,我已经运行了heroku run bash,以了解实际存在的文件,当我执行以下操作时,我发现了一些有趣的信息,即我尝试导入的文件实际上不存在:
看起来我没有在app/blog和app/addressbook中上传这些文件。很有趣,这是一条很好的信息。感谢您的建议……根据您提供的信息,这可能就是答案。在mac终端中(我假设您正在运行OSX的屏幕截图中),从blog.js文件所在的文件夹运行此命令
file blog.js -I
这应该告诉您该文件的mime类型为“text/plain”,如果返回的mime类型为“text/x-c”,则该文件看起来像是最初在Linux上创建的-这是您的问题
要简单地解决这个问题:
- 创建一个新文件
- 将blog.js的内容复制到
- git rm旧文件
- 重命名新文件
- 获取并添加新文件
heroku run bash
并查看该文件是否存在于应用程序希望在Heroku上找到的位置。如果您仍然有问题,请发回这些调查的结果
让我知道
参考资料:奇怪的行为;尝试使用以下工具进行调试:
console.log( __dirname );
确保以下路径正确(指向blog.js文件):
然后尝试显式地将其传递给require
:(可能会根据\uuu dirname
返回的内容而有所不同..)
在我看来,该应用程序运行的工作目录与您在Heroku上预期的不同。
process.cwd()
的值是多少?@myanimal但是只要app.js
和blog.js
在同一个目录中,它就可以正常工作。你是对的!这是一个text/x-c
git rm blog.js
返回的pathspec'blog/blog.js'与任何文件都不匹配
。我可以把它移到垃圾箱吗?您可能需要运行git rm blog/blog.js
,但是可以将它移到垃圾箱。重要的是确保您的包含是mime类型text/plain
。创建一个新文件,并将旧文件的内容复制到其中,然后使用file blog.js-I
命令确保新文件的类型为text/plain
,然后添加并提交,然后推送到Heroku。不知道如何创建非text/plain的文件。每当我创建一个新文件时,它总是将其创建为text/x-c。可能是.js扩展名?嗨,Brad,这真的很奇怪,但是如果删除下面的注释块(第75:84行),您会注意到文件现在报告为“text/plain”。为什么会这样是另一个问题!不是为我做的。还有其他想法吗?以下是日志:Slug编译完成2013-01-26T01:13:01+00:00 heroku[web.1]:使用命令启动进程
node app.js`2013-01-26T01:13:05+00:00 app[web.1]:2013-01-26T01:13:05+00:00 app[web.1]:dirname+/blog/blog.js:/app/blog/blog.js 2013-01-26T01:13:05+00 app[web.1]:错误:找不到模块“/app/blog/blog”2013-01-26T01:13:05+00:00应用程序[web.1]:module.js:340 2013-01-26T01:13:05+00:00应用程序[web.1]:目录名:/app`仍然不工作,似乎正常。正常。我有更多的信息发布在更新后的帖子上。很抱歉,到目前为止,此问题尚未解决!这有点疯狂,我很惊讶问题如此复杂。尝试过这个,我想它告诉我更多的信息。
var blog = require( __dirname + '/blog/blog' ).blog;