I am writing a native addon for NodeJS. How can I use an environment variable as a constant at compile time? That is, "inject" a constant in to the NodeJS C++ addon from an environment variable set during node-gyp build
or npm install
. I found this answer, however as far as I can see, there is no equivalent option for passing through variables to node-gyp
I found that the defines
block and variable expansion in binding.gyp will achieve what I'm after:
{
"targets": [
{
"target_name": "targetName",
"sources": [ "source.cc" ],
"defines": [
'MY_DEFINE="<!(echo $MY_ENV_VAR)"'
]
}
]
}
Then MY_DEFINE
is available with value equal to whatever MY_ENV_VAR
set set to at compile time.