saltyshiomix / nextron

⚡ Next.js + Electron ⚡
https://npm.im/nextron
MIT License
3.69k stars 215 forks source link

How to build the nextron app with seperate env variables? #438

Open jimjacob29 opened 5 months ago

jimjacob29 commented 5 months ago

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.

bm777 commented 5 months ago

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 commented 5 months ago

and of course use it as:

npm run build:staging
npm run build:prod