如何在生产和舞台之间更改 Electron 中的环境变量
How can I change Environments variables in Electron between production and stage
我想根据环境改变API URL。
例如
production: https://example.com
stage: https://stage.example.com
local: https://localhost:3001
在Electron中,如何设置环境变量?
我在构建时尝试更改生产名称但没有用
有了节点你可以使用process.env
.
在您的代码中:
if(process.env.NODE_ENV === 'production') {
// use production api
const api = 'https://example.com';
}
或使用开关盒:
switch (process.env.NODE_ENV) {
case 'production':
// use production api
const api = 'https://example.com';
break;
case 'stage':
// use stage api
const api = 'https://stage.example.com';
break;
case 'local':
// use local api
const api = 'https://localhost:3001';
break;
default:
// use a default this api
}
使用 Electron 时在您的终端中:
$ NODE_ENV=production electron index.js
或将其作为脚本添加到您的 Package.json
"production": "NODE_ENV=production electron index.js",
"stage": "NODE_ENV=stage electron index.js",
"local": "NODE_ENV=local electron index.js"
然后就可以使用了:
$ npm run production
$ npm run stage
$ npm run local
实际上,在您的应用程序打包后,我们无法传递 env 变量。
我的意思是即使我们尝试定义或添加进程环境变量。它在生产中将毫无用处。我会说 process.env.NODE_ENV
在生产模式下将是未定义的。推荐使用electron-is-dev
判断应用是开发模式还是生产模式。
package.json
"production": "electron-builder .",
"stage": "cross-env NODE_ENV=stage electron .",
"local": "cross-env NODE_ENV=development electron ."
你main.js或index.js
const isDev = require('electron-is-dev');
let apiURL = 'https://localhost:3001';
if (isDev) { // or if(process.env.NODE_ENV)
// Dev or Stage
if(process.env.NODE_ENV === 'stage')
apiURL = "https://example.com";
} else {
// Prod mode
apiURL = "https://example.com";
console.log('Running in production');
}
我想根据环境改变API URL。 例如
production: https://example.com
stage: https://stage.example.com
local: https://localhost:3001
在Electron中,如何设置环境变量?
我在构建时尝试更改生产名称但没有用
有了节点你可以使用process.env
.
在您的代码中:
if(process.env.NODE_ENV === 'production') {
// use production api
const api = 'https://example.com';
}
或使用开关盒:
switch (process.env.NODE_ENV) {
case 'production':
// use production api
const api = 'https://example.com';
break;
case 'stage':
// use stage api
const api = 'https://stage.example.com';
break;
case 'local':
// use local api
const api = 'https://localhost:3001';
break;
default:
// use a default this api
}
使用 Electron 时在您的终端中:
$ NODE_ENV=production electron index.js
或将其作为脚本添加到您的 Package.json
"production": "NODE_ENV=production electron index.js",
"stage": "NODE_ENV=stage electron index.js",
"local": "NODE_ENV=local electron index.js"
然后就可以使用了:
$ npm run production
$ npm run stage
$ npm run local
实际上,在您的应用程序打包后,我们无法传递 env 变量。
我的意思是即使我们尝试定义或添加进程环境变量。它在生产中将毫无用处。我会说 process.env.NODE_ENV
在生产模式下将是未定义的。推荐使用electron-is-dev
判断应用是开发模式还是生产模式。
package.json
"production": "electron-builder .",
"stage": "cross-env NODE_ENV=stage electron .",
"local": "cross-env NODE_ENV=development electron ."
你main.js或index.js
const isDev = require('electron-is-dev');
let apiURL = 'https://localhost:3001';
if (isDev) { // or if(process.env.NODE_ENV)
// Dev or Stage
if(process.env.NODE_ENV === 'stage')
apiURL = "https://example.com";
} else {
// Prod mode
apiURL = "https://example.com";
console.log('Running in production');
}