admin管理员组文章数量:1336632
I have an Electron app. I need to get the value of an environment variable from my local machine to use on the renderer thread. I want to pass this value to the renderer thread when the app starts. I know in the main.js
file, I can get the environment variable like this:
main.js
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600
});
win.loadFile('index.html');
// Retrieve the environment variable value
const variableValue = process.env.MY_ENVIRONMENT_VARIABLE;
console.log(`Variable value: ${variableValue}`);
// Send the variable and it's value to the renderer.
win.webContents.send('initialize', { 'variableName':variableValue });
}
The above successfully prints the environment variable's value to the terminal window. However, I need to use this value in the browser side of the ball. How do I get the variable's value there?
Thank you!
I have an Electron app. I need to get the value of an environment variable from my local machine to use on the renderer thread. I want to pass this value to the renderer thread when the app starts. I know in the main.js
file, I can get the environment variable like this:
main.js
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600
});
win.loadFile('index.html');
// Retrieve the environment variable value
const variableValue = process.env.MY_ENVIRONMENT_VARIABLE;
console.log(`Variable value: ${variableValue}`);
// Send the variable and it's value to the renderer.
win.webContents.send('initialize', { 'variableName':variableValue });
}
The above successfully prints the environment variable's value to the terminal window. However, I need to use this value in the browser side of the ball. How do I get the variable's value there?
Thank you!
Share Improve this question asked Dec 2, 2021 at 17:06 DevDev 1,2234 gold badges19 silver badges40 bronze badges1 Answer
Reset to default 7This can't be done directly from the renderer process (with nodeIntegration
off), since it has no direct access to the Node.js process
variable.
The most straightforward way to do this is via the BrowserWindow's preload script, which allows you to expose arbitrary data to the renderer process via Electron's contextBridge
API. An example:
// Main Process
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'); // require `path` if you haven't already
}
});
// Preload Script
const { contextBridge } = require('electron');
// this should print out the value of MY_ENVIRONMENT_VARIABLE
console.log(process.env.MY_ENVIRONMENT_VARIABLE);
contextBridge.exposeInMainWorld('envVars', {
myEnvironmentVariable: process.env.MY_ENVIRONMENT_VARIABLE
});
// Renderer Process
console.log(window.envVars.myEnvironmentVariable)
See the contextBridge API docs: https://www.electronjs/docs/latest/api/context-bridge
本文标签: javascriptSend environment variable value to renderer thread in Electron appStack Overflow
版权声明:本文标题:javascript - Send environment variable value to renderer thread in Electron app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742412287a2470025.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论