The command I run on my server to start my node app is:
sudo IS_PROD=1 node app.js
I have forever installed but can't seem to pass in the environment variable.
sudo IS_PROD=1 forever node app.js
Doesn't seem to do the trick. I have tried several varieties of this. How do I either execute this command successfully or permanently set the environment variable?
First of all you should skip the node thing in you command, it should not be there, you should not be able to execute that. forever automatically starts your script using nodejs. Instead you should do like this;
sudo IS_PROD=1 forever app.js
Probably you, instead of starting your server in foreground, will want to start your server as a daemon. eg.
sudo IS_PROD=1 forever start app.js
This will create a process in the background that will watch your node app and restart it when it exits. For more information see the readme.
Both of these methods preserves the environment variables, just like when you are just using node.
app.js:
console.log(process.env.IS_PROD);
Using node (v0.8.21)
$ node app.js
undefined
$ IS_PROD=1 node app.js
1
$ sudo IS_PROD=1 node app.js
1
Using forever (v0.10.0)
$ forever app.js
undefined
$ IS_PROD=1 forever app.js
1
$ sudo IS_PROD=1 forever app.js
1
Documentation:
process.env
An object containing the user environment. See environ(7).
Related
Just experimenting with dtruss/dtrace here. I have this command:
sudo dtruss node server.js &> my-dtruss.log
that log looks like this: https://gist.github.com/ORESoftware/9b4d47682a8f0ec25330c02b4ef3ea2d
my question is, if my NODE_ENV var is set, why is it unset for child processes of dtruss command? and how can I set env variables for the node process?
my node server crashes if NODE_ENV is not set, so that's why I assume the dtruss output ends and does not continue.
I am installing an SailsJS app on an Amazon Ubuntu instance.
If I run the app with sails lift everything works and the program run successfully. Now I try to run it through forever for obvious reasons. As specified by the SailsJS documentation I run forever on the app.js file located at the root of my project : forever start app.js
But that time the app fails :
forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] XXXX /usr/bin/nodejs app.js 15348 15350 /home/ubuntu/.forever/XXXX.log STOPPED
Logfile contains only :
error: Forever detected script exited with code: 0
Any idea why sails lift would work but not with forever ?
Sometimes there are permissions errors. Try to verify forver's permissions files in .forever directory or maybe run with sudo.
I run forever start app.js with sudo and I did't have any problems.
After that sudo forever list a get:
data: [0] kBdX /usr/bin/nodejs app.js 23533 23538 /home/ubuntu/.forever/kBdX.log 0:0:34:28.312
I used to start my application using:
DEBUG=chakka ENVIRONMENT=production npm start
How can i start it using forever so i wouldn't have to do it everytime i want to test the application? Thanks!
First you need to know what the application's main script file is. Open up your package.json file and find out what the start script is. If you're using Express it might be app.js. So we'll assume app.js for this example, replace with whatever your file is.
To start the application:
DEBUG=chakka ENVIRONMENT=production forever start app.js
to restart the application after you've made changes:
forever restart app.js
I'm currently using forever to run my node.js application in our development environment. What I am currently struggling with is how to pass node.js arguments when using "forever start"
Here is an example where I need to pass a number and a date to node. It's not working so any help would be appreciated.
forever -c 'node 8010 "2014-11-11 12:00:00"' start app.js
According to the documentation, the script arguments come after the call.
https://github.com/nodejitsu/forever
usage: forever [action] [options] SCRIPT [script-options]
forever start app.js 8010 "2014-11-11 12:00:00"
The usage of nconf https://github.com/flatiron/nconf in your project is highly recommended to grab those params.
I start my node app like this:
node --stack-size=32000 app.js
How can I use forever to start up my app with the appropriate --stack-size option?
Enclose the node command in quotes with the -c command:
forever start -c "node --stack-size=32000" app.js