6

Let's say there is a variable key1 and its value is 123

key1=123

so when I run the command in linux environment echo $key1, I get output as 123.

enter image description here

Now I have the following gulp task.

const child_process = require('child_process');
....
gulp.task('printKeyValue', function() {
    var value1 = child_process.execSync('echo $key1');
    console.log(value1.toString().trim());
});

Here, I'm trying to access value of linux variable from nodejs by using Child Process

But when I run the following gulp task, I don't get the desired output.

npm run gulp -- printKeyValue

Instead I get output as $key1 and not 123.

See below screenshot

enter image description here

Other commands like ls & pwd in gulp task gives the desired output.

Can some one please help on this or suggest an alternate way?

jww
  • 97,681
  • 90
  • 411
  • 885
Siddarth
  • 351
  • 2
  • 6
  • 20
  • 3
    Possible duplicate of [Read environment variables in Node.js](https://stackoverflow.com/questions/4870328/read-environment-variables-in-node-js) – Amadan Oct 16 '18 at 11:39
  • Actually not sure if duplicate. Do you want to access an environment variable in Node.js like your title says, or in a subprocess command line like your code says? – Amadan Oct 16 '18 at 11:41
  • @Amadan Thank you for redirecting me to an alternate solution. After looking the solution, I tried `console.log(process.env.key1);` I get the output as `undefined` which is not the expected output. – Siddarth Oct 16 '18 at 11:46
  • [Can't read my environment variable in my Node.js app](https://stackoverflow.com/q/10803653/608639), [Read environment variables in Node.js](https://stackoverflow.com/q/4870328/608639) – jww Oct 17 '18 at 07:47
  • 3
    Possible duplicate of [Can't read my environment variable in my Node.js app](https://stackoverflow.com/questions/10803653/cant-read-my-environment-variable-in-my-node-js-app) – Makyen Oct 18 '18 at 00:07

1 Answers1

11

You are not exporting the variable. When you just do

key1=123

the variable is not propagated to subprocesses. It will be available in your current bash process, so you can see it when you type echo $key1, but it will not get inherited by the node process. As man bash says:

When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate execution environment that consists of the following. Unless otherwise noted, the values are inherited from the shell.

  • [...]

  • shell variables and functions marked for export, along with variables exported for the command, passed in the environment

You need to either define the variable as exported

export key1=123

or mark an existing variable for export

key1=123
export key1

or launch your node with the modified environment, either via the bash innate capability to do so

key1=123 node code.js

or using /usr/bin/env utility:

env key1=123 node code.js

Once the variable is properly passed to the node process, it will be available both in process.env.key1 and as $key1 in a child process.

EDIT: I just noticed, you actually gave the command you're running; it does not matter, the same logic goes for every executable, whether node or npm or anything else.

Community
  • 1
  • 1
Amadan
  • 191,408
  • 23
  • 240
  • 301