Search code examples
node.jsexpressnpmnpm-startdev-to-production

How do run an express-generator created application in production mode?


I'm using the Express application generator, which recommends starting the server in this manner:

$ DEBUG=myapp:* npm start

This works and my app starts to listen on the specified port.

However, the DEBUG makes me wonder: is this running in 'dev' mode? If so, how do I run it in production mode when deployed on the server?

If I only run npm start, it gets stuck:

$ npm start

> myapp@0.0.0 start /home/aalaap/projects/myapp
> node ./bin/www

There is no line saying that the server is listening and the app is not accessible.

If I run node app.js, it exits immediately without any output.

I feel that running the app in debug mode may not be what is ideal for production, but I may be wrong about that if I haven't understood this correctly.

Edit: On the server, there's a cron entry that restarts the server in intervals in case it crashes. If the above method is the only way to run it, should I modify the cron command to include the DEBUG part?


Solution

  • Use the NODE_ENV environment variable:

    $ NODE_ENV=production npm start 
    

    The DEBUG variable you mention is only for log output and configures what exactly you would like to see in the logs.

    The question why your application hangs can’t be answered without seeing any code and debugging into it. I hardly doubt that it is related to the environment variables.