nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.44k stars 276 forks source link

Is there any way (other than process.execArgv) to access runtime conditions #4324

Closed znewsham closed 5 days ago

znewsham commented 5 months ago

Details

Our application is executed with some conditions, like so:

node whatever.js --conditions=someArbitraryCondition

I have two questions:

  1. is there any way to access those conditions (and the standard import/require/node conditions) as a set within a module?
  2. is there any way of passing in custom conditions to either the import(...) call or import.meta.resolve

(2) I suspect will be no, and possibly with really good reason - the use case here would be that in some cases (e.g., testing) we import client side functionality to the server - the module in question defines a client import and no server import (mostly this is correct) - but in this specific case it would be nice to (for example) define that it's running within a test, and as such should use a test entry point. Or, "fake" that it's being imported from the browser.

Thanks!

Node.js version

18 - if it works on some other version than 18 that'd be good to know too.

Example code

No response

Operating system

n/a - but I'm on linux

Scope

runtime

Module and version

Not applicable.

RedYetiDev commented 5 days ago

Node.js provides a util.parseArgs method to parse arguments, other than that, packages or process.execArgv will be your best bet.