请考虑以下几点。
node file1.js && react-scripts start我试图在file1.js中调用GCP秘密管理器。在收到请求后,我希望将它们设置为process.env下的环境变量。在那之后,我想在前面访问他们。在没有OAuth的情况下,浏览器无法调用那个秘密管理器。有办法在这两个脚本之间共享process.env吗?
File1代码
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
// Instantiates a client
const client = new SecretManagerServiceClient();
const firebaseKeysResourceId = 'URL'
const getFireBaseKeys=async()=> {
const [version] = await client.accessSecretVersion({
name: firebaseKeysResourceId,
});
// Extract the payload as a string.
const payload = JSON.parse(version?.payload?.data?.toString() || '');
process.env.TEST= payload.TEST
return payload
}
getFireBaseKeys()发布于 2021-06-30 16:47:26
扩展我的评论
方法1 -一种整洁但不需要的方法
假设您在环境中拥有您想要的这些vars:
const passAlong = {
FOO: 'bar',
OAUTH: 'easy-crack',
N: 'eat'
}在file1.js的末尾,您可以这样做
console.log(JSON.stringify(passAlong));Note您不能在file1.js中打印任何其他内容
然后你会像这样调用你的脚本
PASSALONG=$(node file1.js) react-script start在react开始时,您可以这样做,将传递的变量填充到环境中。
const passAlong = JSON.parse(process.env.PASSALONG);
Object.assign(process.env,passAlong);方法2 -我会做什么
使用派生方法只需在file1.js中设置您喜欢的process.env,然后在file1.js末尾添加类似的内容
// somewhere along the way
process.env.FOO = 'bar';
process.env.OAUTH = 'easy-crack';
process.env.N = 'eat';
// at the end of the script
require('child_process').spawnSync(
'node', // Calling a node script is really calling node
[ // with the script path as the first argument
'/path/to/react-script', // Using relative path will be relative
'start' // to where you call this from
],
{ stdio: 'inherit' }
);https://stackoverflow.com/questions/68198060
复制相似问题