简体   繁体   中英

Changing environment variables of terminal from node.js

I'm working on a simple node.js CLI that provides functionality for (MF) authentication for AWS. This requires on-calling the AWS CLI and updating environment variables in the current terminal (in order to allow direct calls to the aws cli or other consumers of the aws cli to call it).

Is there any way to update the environment variables so they can be made available to the calling terminal? Neither process.env['KEY'] = 'VALUE' nor require('child_process').exec('export KEY=VALUE', ...) works, as they seem to be isolated to the current or child process and not the executing terminal.

Solutions or alternatives that still allow a simple call through the node.js CLI would be much appreciated! I've already got a bash script that does this, but wanted to allow calls through node so that it's easier to install and provides a consistent interface with other functionality.

OS: currently linux, possibly also windows later. Node Version: 10+

If I understand your question correctly you are trying to pass environment variables from a child process back to a parent process. This is not possible. However it might be worth a try to spit out the environment variable as a string output and use this to source the environment in the parent process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM