Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing command line arguments to Node? #2563

Open
grahamb opened this issue Aug 30, 2024 · 6 comments
Open

Passing command line arguments to Node? #2563

grahamb opened this issue Aug 30, 2024 · 6 comments

Comments

@grahamb
Copy link

grahamb commented Aug 30, 2024

Issue report

Question 1: What is the problem?
There doesn't seem to be a way to pass command-line arguments to node when invoked by Passenger.

Example: Since NodeJS v20.6.0, node supports an --env-file .env option. The .env file will be parsed into process.env. This reduces the need to depend on third-party modules like dotenv.

I can not find any configuration options to tell Passenger to use the --env-file argument when it invokes node.

I tried using PassengerAppStartCommand set to /usr/local/bin/node --env-file /path/to/app/.env /path/to/app/index.js. This launched the app, but not in the usual way. I could curl is on its native port (3000), but apache was not serving the app on port 80.

Question 2: Passenger version and integration mode:
Passenger 6.0.23, Apache httpd integration

Question 3: OS or Linux distro, platform (including version):
Red Hat Enterprise Linux release 9.4 (Plow)

Question 4: Passenger installation method:

Your answer:
Phusion YUM repo

Question 5: Your app's programming language (including any version managers) and framework (including versions):
NodeJS 20.17.0, Express 4.19.2

@CamJN
Copy link
Member

CamJN commented Sep 1, 2024

I recommend using env vars to pass flags to node.

For example:
SetEnv NODE_OPTIONS "--env-file /path/to/app/.env"

more info:
https://nodejs.org/api/cli.html#cli_node_options_options
https://httpd.apache.org/docs/2.4/mod/mod_env.html#setenv

@grahamb
Copy link
Author

grahamb commented Sep 1, 2024

Unfortunately, you can't pass --env-file via NODE_OPTIONS because you can specify NODE_OPTIONS in an .env file. nodejs/node#51147

 /tmp/foo > NODE_OPTIONS="--env-file=.env" node
node: --env-file= is not allowed in NODE_OPTIONS

@CamJN
Copy link
Member

CamJN commented Sep 1, 2024

Ah, that sucks, well then I guess the only way forward is to use the Generic Language Support feature: https://www.phusionpassenger.com/docs/advanced_guides/gls/generic_guide.html

@grahamb
Copy link
Author

grahamb commented Sep 1, 2024

As I mentioned in the issue, I tried that using PassengerAppStartCommand, and can't seem to get it working under Passenger with Apache.

@CamJN
Copy link
Member

CamJN commented Sep 1, 2024

You need to pass the port for your app to listen on in the start command. It must be passed this way because passenger chooses the port per-process. I don't see any way that you are doing that.

@grahamb
Copy link
Author

grahamb commented Sep 3, 2024

I was able to get it working with PassengerAppStartCommand "/usr/bin/env VIRTUAL_PORT=$PORT /usr/local/bin/node --env-file /var/node/app/current/.env /var/node/app/current/index.js"

It would be great if there was the ability to pass startup arguments directly through an option when in Apache or Nginx mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants