In bash I can see npm environment variables with npm run env. USER=brianmackey is one such environment variable. How can I set an environment variable, say USER=billybob?
I know I can use npm config set <key> <value> [--global]. Is key+value always/in any case an environment variable? Can I set the environment variables in session?
Single Command
If you want to set environment variables for a single node command, you can simply do this:
$ USER=billybob node server.js
Loaded for each session
If you want to permanently set that environment variable for your user, edit your ~/.bash_profile and add the following line:
export USER="billybob"
This will automatically set the given environment variable each time you create a new terminal session.
Existing for the entire current session
Lastly, if you want to set the environment variable only for the current session, just run it as it's own command:
$ USER=billybob
$ node app.js # user is billybob
$ node app.js # user is still billybob
When you exit the session, these temporarily set environment variables will be cleared.
Related
I have a singularity container built with a scientific filesystem app.
I run the container with singularity run --app myapp image.sif and, accordingly, I have the environment variable SCIF_APPDATA=/scif/data/myapp set inside the container.
I'd like to bind a path using this environment variable. Something like:
singularity run -B /my/path/on/host/:$SCIF_APPDATA/input/
Unfortunately this does not work. I didn't manage to make singularity use as a mount path the environment variable with its "internal" value.
I have to explicitly pass the value of the environment variable:
singularity run -B /my/path/on/host/:/scif/data/myapp/input
Does anybody know how to use container environment variables in bind paths?
I don't think it is possible to directly use environment variables from inside the container in the bind statement. However, you can do it in two steps, first a call to singularity exec to get the value of the variable, then use it with singularity run:
SCIF_APPDATA=$(singularity exec ascisoftApp.sif bash -c "echo \$SCIF_APPDATA")
singularity run -B /my/path/on/host/:$SCIF_APPDATA/input/ ...
Don't forget the backslash to escape the $ in the echo command.
Note: Not sure why but it doesn't work for me when I directly use echo, hence the wrapping with bash -c.
I have the following node script to start a react app. It sets a variamle in the .env. Is there a way to set this script in the package.json, so it can take a parameter from the terminal?
script in the terminal that sets a color in the .env:
set COLOR=blue&& node scripts/start.js
package.json, how to hand over the color parameter?
"scripts": {
"startWithColor": "set COLOR=$COLOR&& node scripts/start.js",
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?
https://docs.npmjs.com/cli/cache
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...
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:
...
"scripts": {
"print-env-vars": "printenv | grep \"^npm_\""
},
...
Then run the following command:
npm run print-env-vars
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 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 effective root config by checking the npm_config_root environment variable.
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:
"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
I've run into an issue with openshift - after setting the environment variable over rhc env set JAVA_OPTS_EXT=" -D spring.profile.active=production" my ssh access broke down giving me weird access rights error. Some ideas here?
I don't know for what reason after setting a different value onto the JAVA_OPTS_EXT it locked me out of permissions. It was sufficient to unset the variable and set it again to a desired value. Everything started to work smoothly again afterwards.
A command to unset the environment variable: "rhc env unset {VARIABLE1} -a {APP_NAME}"
A command to set the environment variable: "rhc env set {VARIABLE1}={VALUE1} -a {APP_NAME}"
For further info about the manipulation of environmnet variables refer to https://developers.openshift.com/managing-your-applications/environment-variables.html
I've set an environment variable (NPM_TOKEN) for my repo in Docker Cloud to use when building my Dockerfile. However, the variable is always empty...
Tried both of these in Dockerfile:
RUN echo ${NPM_TOKEN}
and:
ARG NPM_TOKEN
RUN echo ${NPM_TOKEN}
Am I wrong in assuming that Docker Clouds environment variables for build does the same thing as --build-arg?
It took me along time, but you can use build hooks to set variables for automated builds!
https://docs.docker.com/docker-cloud/builds/advanced/#build-hook-examples