I have a single environment variable that I need to set during preinstall (my npm token is needed in .npmrc to fetch a private package). This particular env var is stored within a config service I am using (Firebase) and so it needs to be fetched from the service and then added to the env.
I've tried all manner of ways, such as fetching it in a node script and trying to capture it via stdout:
const { spawnSync } = require( 'child_process' );
const ls = spawnSync( 'firebase', [ 'functions:config:get', 'npm.read_only' ] );
JSON.parse(ls.stdout.toString());
// Printing this shows the correct token
My latest try was this:
#!/bin/sh
echo 'Setting up development env';
NPM_READ_ONLY_TOKEN=`firebase functions:config:get npm.read_only`
export NPM_READ_ONLY_TOKEN=$(eval echo $NPM_READ_ONLY_TOKEN)
echo 'NPM_READ_ONLY_TOKEN => '$NPM_READ_ONLY_TOKEN;
# Prints the correct token on the screen
exit 0;
But, when running "./configEnv.sh && echo $NPM_READ_ONLY_TOKEN", the env var is empty.
How can I achieve this?
If I'm reading your question right, you wish to fetch your token by executing configEnv.sh, and then use that token down the stream.
The problem you're experiencing right now is related to the scope of environment variables being limited to the current process (and its children, if variables are exported). Environment variables can never be propagated back to parent shell.
When executing your list ./configEnv.sh && echo $TOKEN, configEnv.sh is executed in its own (separate) subshell, so the next process in list (echo ..) doesn't receive the token you set.
One way around this is to execute your token fetching (and setting) routine in the same shell, simply by sourcing the configEnv.sh:
#!/bin/sh
# Usage: . configEnv.sh
echo 'Setting up development env';
export NPM_READ_ONLY_TOKEN=$(firebase functions:config:get npm.read_only)
and then using the token in the same shell (or its child):
. configEnv.sh && echo "$NPM_READ_ONLY_TOKEN"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With