nextron icon indicating copy to clipboard operation
nextron copied to clipboard

How to build the nextron app with seperate env variables?

Open jimjacob29 opened this issue 1 year ago • 2 comments

Assist me with the following task, please. I currently use nextron build --all for building. However, whenever I build for staging, I find myself manually updating redirection URLs and API settings to match the staging environment. Similarly, for the release version, I need to manually switch these settings to production. Can you guide me on how to build for each environment separately using npm commands such as npm run build:staging and npm run build:prod? I attempted to incorporate the dotenv package and configure it similar to my React app, but it's not functioning as expected.

jimjacob29 avatar Jan 23 '24 05:01 jimjacob29

if we consider that you have two env: prod and staging. You can use dotenv and cross-env easily for that, but it has to be configured in next.config.js, and finally play in your packages.json file

You must have 2 .env file prod and staging -> .env.prod & .env.staging

Example of content of env.prod

BASE_URL="url-prod"

Example of content of env.stagnig

BASE_URL="url-staging"

And modify your next.config.js file, to handle the dynamic env load via the NODE_ENV

const { parsed: localEnv } = require('dotenv').config({
  path: `./.env.${process.env.NODE_ENV}`
});

module.exports = {
  env: {
    BASE_URL: localEnv.BASE_URL,
  },
  // Other configurations
};

packages.json

"scripts": {
  "build:staging": "cross-env NODE_ENV=staging nextron build --all",
  "build:prod": "cross-env NODE_ENV=production nextron build --all",
  "build:linux": "nextron build --linux --x64",
  // Other scripts...

}

bm777 avatar Jan 29 '24 09:01 bm777

and of course use it as:

npm run build:staging
npm run build:prod

bm777 avatar Jan 29 '24 09:01 bm777