erwin erwin - 5 days ago 6
Node.js Question

ShipIt node/express deployment with PM2 - cannot set the correct NODE_ENV

I am trying to start my node app in the correct environment upon deployment with ShipIt ( w ships-npm plugin) ? I am deploying it in the staging env, but the app starts in development mode as stated displaying yj-he process.env.NODE_ENV

deployment with ships

>$ shipit staging deploy
Starting deployment...
....
Running 'start_server' task...
Running "cd /opt/hello/releases/20161128182300 && npm start" on host "myhost.live".
@myhost.live
@myhost.live > hello-world-express@0.0.1 start /opt/hello/releases/20161128182300
@myhost.live > pm2 startOrReload ecosystem.json
@myhost.live
@myhost.live [PM2] Applying action reloadProcessId on app [hello](ids: 0)
@myhost.live [PM2] [hello](0) ✓
@myhost.live ┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬──────────
@myhost.live │ App name │ id │ mode │ pid │ status │ restart │ uptime


├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────┼──────────
@myhost.live │ hello │ 0 │ fork │ 7224 │ online │ 2 │ 0s

└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────┴──────────
@myhost16.live Use
pm2 show <id|name>
to get more details about an app
Finished 'start_server' after 9.01 s

I thought that deploying in 'staging' mode would set the NODE_ENV to 'staging'... not sure

hello.js

var express = require('express');
var app = express();

app.get('/', function (req, res) {
res.send('Hello World! Now you can call Express at 3637');
});

var port = process.env.PORT || 3637;

app.listen(port);
console.log('Now on ' + process.env.NODE_ENV + ' server');
console.log('Express app listening on localhost:'+ port);


The console log states :

0|hello | Now on development server
0|hello | Express app listening on localhost:3637


shipitfile.js

...
// this task starts the application with PM2
shipit.blTask('start_server', function () {
var cwd = shipit.releasePath;
return shipit.remote( "cd " + cwd + " && npm start");
});

shipit.on('deployed', function () {
console.log("Deployed !");
shipit.start('start_server');
});
...


package.json

...
"main": "hello.js",
"scripts": {
"start": "pm2 startOrReload ecosystem.json",
...


ecosystem.json

{
"apps" : [
{
"name": "hello",
"cwd": "/opt/hello/current",
"script": "hello.js",
"args": "",
"watch": true,
"node_args": "",
"merge_logs": true,
"env": {
"NODE_ENV": "development"
},
"env_production": {
"NODE_ENV": "production"
},
"env_staging": {
"NODE_ENV": "staging"
}
}]
}


what's wrong with my ecosystem.js file ?

thanks for feedback

Answer

With PM2, to use the production environment variables (set in env_production), you need to specify the --env option.

Here you can find more information about it.

To solve your issue just add the --env production to your start attribute in the package.json:

"start": "pm2 startOrReload ecosystem.json --env production",
Comments