I get get the NPM cache location using:
cache_location="$(npm get cache)"
however, is this value also represented by an env variable that I can read?
Something like NPM_CACHE_LOCATION
?
I get get the NPM cache location using:
cache_location="$(npm get cache)"
however, is this value also represented by an env variable that I can read?
Something like NPM_CACHE_LOCATION
?
Short answer: It depends on when/how you want to access it, as there is no env variable, (e.g.
NPM_CACHE_LOCATION
), available whilst npm is not running.You'll need to invoke
npm config get cache
ornpm get cache
as you are currently doing.However, once npm is running the configuration parameters are put into the environment with the
npm_
prefix.The following demonstrates this...
Discover which env variables are available:
As a way to find out what env variable(s) npm puts in the environment, you can utilize printenv in an npm-script. For example in package.json add:
Then run the following command:
Get the cache location via an env variable:
In the resultant log to the console, (i.e. after running
npm run print-env-vars
), you'll see that there's thenpm_config_cache
environment variable listed. It reads something like this:In the docs it states:
Note: Running
printenv | grep "^npm_"
directly via the CLI returns nothing.Accessing the cache location with env variable:
You can access the cache location via an npm-script, For example:
See cross-var for utilizing a cross-platforms syntax.
Accessing the npm cache location via a Nodejs script. For example:
Note: This node script will need to be invoked via an npm-script for the
process.env.npm_config_cache
to be available. Invoking it via the command line running, e.g.node ./somefile.js
will returnundefined
- this further demonstrates that the parameters with the_npm
prefix are only put into the environment whilst npm is running.Not ideal, however you could set your own environment variable using export of course:
and unset to remove it: