Read command line arguments passed to node.js using pm2 - node.js

I'm aware about how to pass variables to node.js using pm2. But how do I read them? process.argv doesn't contain it.
This is what I'm referring to.
Passing environment variables to node.js using pm2
UPDATE
pm2 start file_name.js -- -my_port 8080 is the right way to do it. process.argv will contain the arguments.
But running pm2 describe file_name still shows args -3000 which is a cached value.
Restarting the system gives me the argument that was passed last before restart, which was 3000 in my case.

I think you're confusing :
node_args
node_args list ["--harmony", "--max-stack-size=1024"] arguments given to node when it is launched
Those are node executable options, like --harmony or --debug=7001. For more informations see node --help
args
args list ["--enable-logs", "-n", "15"] arguments given to your app when it is launched
Those are your script arguments. In a json declaration it's the arg property but within command line the syntax is:
pm2 start app.js -- arg1 arg2
Those should be available in process.argv.
Reference

From the Node.js doc about process.argv:
An array containing the command line arguments.
It does not contain environment variables. You can access the ENV_VARIABLE environment variable using
process.env.ENV_VARIABLE
See this answer.

Related

How to pass execution arguments to exe using PM2?

I am using pm2 to start a .exe but I'm not able to pass argument to it.
The command I am using is pm2 start go-cqhttp_windows_amd64.exe -- -faststart. But it doesn't work.I want to pass the parameter -faststart to this program.Thanks!

how start actionhero by pm2

I'm starting actionhero by this command
pm2 start .\node_modules\actionhero\bin\actionhero
But actionhero doesn't start successfully and this is in my pm2 log:
error: No config directory found in this project, specified with
--config, or found in process.env.ACTIONHERO_CONFIG
I have no experience with actionhero but it clearly says no config directory found.
Either
1. --config parameter has to be passed as next argument for pm2 start or
2. Set an env variable ACTIONHERO_CONFIG to appropriate value.
Boot Options to find the Config Directory
When launching ActionHero you can specify which config directory to use with --config '/path/to/dir' or the environment variable ACTIONHERO_CONFIG, otherwise ./config/ will be used from your working directory.
The priority of arguments is:
Use the project's ./config folder, if it exists.
actionhero --config=PATH1 --config=PATH2 --config=PATH3,PATH4
ACTIONHERO_CONFIG=PATH1,PATH2 npm start
Note that if --config or ACTIONHERO_CONFIG are used, they overwrite the use of the default /config folder. If you wish to use both, you need to re-specify "config", e.g. --config=config,local-config. Also, note that specifying multiple --config options on the command line does exactly the same thing as using one parameter with comma separators, however the environment variable method only supports the comma-delimited syntax.

How to get path of nodejs executable on runtime

I have to execute a node command within another node process as shown below:
require('child_process').exec (`${<path of current node executable>} someModule`);
How can i retrieve the node executable path on runtime to execute this command.
process.execPath should be what you require.
process.argv0 is not alway pointed to node binary.
As the in the official Node document.
The process.argv0 property stores a read-only copy of the original value of argv[0] passed when Node.js starts.
In the example of the official document, it demo the case that process.argv0 is not the node binary. customArgv0 is for exec's -a flag.
$ bash -c 'exec -a customArgv0 ./node'
> process.argv[0]
'/Volumes/code/external/node/out/Release/node'
> process.argv0
'customArgv0'
If you are trying to execute another node app, how about taking a look at child_process.fork?
You code then should be as follows.
// fork()'s first argument should be the same as require();
// except that fork() executes the module in a child process
require('child_process').fork(`someModule`);
As stated in document, fork() use the same node binary as process.execPath, or you can specify other node binary to execute the module.
By default, child_process.fork() will spawn new Node.js instances
using the process.execPath of the parent process. The execPath
property in the options object allows for an alternative execution
path to be used.

How to use pm2 with a nodejs app that uses readline for taking command line input?

I have a Node.js app that uses the node's native readline to be able to take command-line inputs.
When launching the app with pm2, the command-line input is unavailable.
Any ideas how to solve this issue? Other than using systemd and creating an init script myself?
Use pm2 to attach to your process and you will see readline, clearline and cursorTo working as expected.
First get your process id with:
$ pm2 id {your-process-name}
[ 7 ]
Let's say it's 7:
$ pm2 attach 7
if you check the pm2 website they clearly mention the following line: Advanced, production process manager for Node.js. So using it in this context is unnecessary as all pm2 does is start your 'node' process and allows you to manage it, the simple way is to use command line args while starting the process.
for example:
I myself use commander for this purpose. it manages all my command line arguments (u can see its usage). and with pm2 i use it like following:
pm2 start server.js --name production -- --env dev -p 3458
notice -- before --env, it is used to separate pm2 arguments from the arguments you want to supply to your process
p.s.
PM2 has more complex usage than this, in the terms of process management, i myself use it for production level deployment. If you want to take input from a user every time s/he starts your app, then you should stick with using node command only

Docker & PM2: String based CMD with environment variables

I'm currently using the shell-form of CMD in Docker for launching my node app:
CMD /usr/src/app/node_modules/.bin/trifid --config $TRIFID_CONFIG
The env-var TRIFID_CONFIGis set to a default in the Dockerfile:
ENV TRIFID_CONFIG config.customer.json
This makes it easy to pass another config file for dev-environments for example.
Now I try to switch this to PM2 for production. However it looks like all PM2 samples are using the "exec" form which from what I understood does not evaluate ENV-vars. I tried the shell-form with PM2:
CMD pm2-docker /usr/src/app/node_modules/trifid/server.js --config $TRIFID_CONFIG
But it looks like the variable is not evaluated like this, it fails back to default on execution.
What would be the proper way to handle this with PM2 inside a Docker image?
I had a discussion on Github and meanwhile figured it out:
CMD pm2-docker /usr/src/app/node_modules/.bin/trifid -- --config $TRIFID_CONFIG
So the trick is to use -- after the command and the rest will be passed as argument. If I use the shell form env-vars do seem to get evaluated properly.

Resources