节点如何在多次运行之间共享process.env?
Node How to share process.env between multiple runs?
考虑以下因素。
node file1.js && react-scripts start
我正在尝试对 file1.js 中的 GCP Secret Manager 进行 API 调用。收到请求后,我想将它们设置为process.env下的环境变量。之后,我想在前端访问它们。没有 OAuth,浏览器无法调用该 Secret Manager。有什么办法可以在这两个脚本之间共享 process.env 吗?
文件1代码
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()
扩展我的评论
方法 1 - 有点简洁但不必要
假设您在环境中有这些变量:
const passAlong = {
FOO: 'bar',
OAUTH: 'easy-crack',
N: 'eat'
}
然后在 file1.js 结束时你会这样做
console.log(JSON.stringify(passAlong));
注意 你不能在 file1.js
中打印 任何东西
然后你会像这样调用你的脚本
PASSALONG=$(node file1.js) react-script start
并且在 react-script 的开头,您将执行此操作以将传递的变量填充到环境中。
const passAlong = JSON.parse(process.env.PASSALONG);
Object.assign(process.env,passAlong);
方法二 - 我会怎么做
使用 spawn 方法只需要在 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' }
);
考虑以下因素。
node file1.js && react-scripts start
我正在尝试对 file1.js 中的 GCP Secret Manager 进行 API 调用。收到请求后,我想将它们设置为process.env下的环境变量。之后,我想在前端访问它们。没有 OAuth,浏览器无法调用该 Secret Manager。有什么办法可以在这两个脚本之间共享 process.env 吗?
文件1代码
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()
扩展我的评论
方法 1 - 有点简洁但不必要
假设您在环境中有这些变量:
const passAlong = {
FOO: 'bar',
OAUTH: 'easy-crack',
N: 'eat'
}
然后在 file1.js 结束时你会这样做
console.log(JSON.stringify(passAlong));
注意 你不能在 file1.js
中打印 任何东西然后你会像这样调用你的脚本
PASSALONG=$(node file1.js) react-script start
并且在 react-script 的开头,您将执行此操作以将传递的变量填充到环境中。
const passAlong = JSON.parse(process.env.PASSALONG);
Object.assign(process.env,passAlong);
方法二 - 我会怎么做
使用 spawn 方法只需要在 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' }
);