UDIT SONI
UDIT SONI

Reputation: 81

How to access parent global variable in child process nodejs

I have the following code:

import ChildProcess = require("child_process");
global.abc = "token";
ChildProcess.spawn("node", [path.join(process.cwd(), "./install-db.js")]);

install-db.js in this file I am not able to get global variable, How should I use global.abc in this child process

Upvotes: 0

Views: 4056

Answers (1)

Farhan Tahir
Farhan Tahir

Reputation: 2144

As child process is a separate entity, you can't excess your main process's global variable inside it.

Though there are ways to send data/inputs to child processes. You can use command line arguments to send data to child process.

Read more about passing arguments to child process: https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Upvotes: 1

Related Questions