Search code examples
javascriptnode.jslinuxexpressforever

Node.js works when run with node command but not with forever


I have a Node.js app that I can run fine with this command: node app.js. It starts up and I can go to my server address and see it the app working. However, when I try to run the app with forever (to keep the app running on my server even when I'm not logged in), using this command: ./forever start ../../../app.js I get the following error in the log file:

Error: Failed to lookup view "home" in views directory "/root/ExerciseApp/node_modules/forever/bin/views"

Here's my directory structure:

ExerciseApp
  /nbproject
    project.properties
    project.xml
  /public
    /css
      style.css
    /scripts
      buttons.js
  /views
    /layouts
      main.handlebars
    home.handlebars
    update.handlebars
  app.js
  .gitignore
  dbcon.js
  package.json

Here's the relevant parts of app.js:

//*****Express stuff*********
var express = require('express');
var app = express();
app.use(express.static('public'));

//*****BodyParser stuff*******
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

//*****Handlebars stuff******
//Create instance of handlebars let it know default layout is 'main'
//Default layout is the area all the other contents will be inserted
var handlebars = require('express-handlebars').create({defaultLayout:'main'});
//.handlebars extensions are managed by handlebars
app.engine('handlebars', handlebars.engine);
//Lets us ignore .handlebars extensions
app.set('view engine', 'handlebars');

var helpers = require('handlebars-helpers')();
var moment = require('moment');

//*****MySQL stuff******
var mysql = require('./dbcon.js');

app.set('port', 3645);

If I do the command ./forever list, I can see that the app is running but visiting the page doesn't work.

EDIT: I'm running this on Debian Linux


Solution

  • Unless you provide more data, and assuming the operating system is Linux and forever is installed with npm install forever -g, what you should do is forever start [../path/to/filename], not ./forever (which would try to run a binary called 'forever' inside the directory where you are at.

    You have two choices:

    a) run forever from the main directory the way I told you

    b) use absolute paths in your app.js, or relative paths to ../../../../node_modules/forever

    I recommend a).