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
or npm 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...
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:
...
"scripts": {
"print-env-vars": "printenv | grep \"^npm_\""
},
...
Then run the following command:
npm run print-env-vars
In the resultant log to the console, (i.e. after running npm run print-env-vars
), you'll see that there's the npm_config_cache
environment variable listed. It reads something like this:
npm_config_cache=/Users/UserName/.npm
In the docs it states:
configuration
Configuration parameters are put in the environment with the
npm_config_
prefix. For instance, you can view the effectiveroot
config by checking thenpm_config_root
environment variable.
Note: Running printenv | grep "^npm_"
directly via the CLI returns nothing.
You can access the cache location via an npm-script, For example:
"scripts": {
"cache-loc-using-bash": "echo $npm_config_cache",
"cache-loc-using-win": "echo %npm_config_cache%"
},
See cross-var for utilizing a cross-platforms syntax.
Accessing the npm cache location via a Nodejs script. For example:
const cacheLocation = process.env.npm_config_cache;
console.log(cacheLocation)
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 return undefined
- 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:
export NPM_CACHE_LOCATION="$(npm get cache)"
and unset to remove it:
unset NPM_CACHE_LOCATION