Ad

How Can I Divide Prod And Dev Version In Create-react-app

I have an application on the react, when the build I want insert another path, how can this be done?

I have two files with paths that need to be changed

enter image description here ........................................................................................................................................ config.js

const config = {
  url: 'http://45.86.181.235' // here
};

export default config;

package.json

 {
  "name": "celbine",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/core": "7.5.5",
    "@svgr/webpack": "4.3.2",
    "@typescript-eslint/eslint-plugin": "1.13.0",
    "@typescript-eslint/parser": "1.13.0",
    "axios": "^0.19.0",
    "babel-eslint": "10.0.2",
    "babel-jest": "^24.8.0",
    "babel-loader": "8.0.6",
    "babel-plugin-named-asset-import": "^0.3.3",
    "babel-preset-react-app": "^9.0.1",
    "camelcase": "^5.2.0",
    "case-sensitive-paths-webpack-plugin": "2.2.0",
    "chart.js": "^2.8.0",
    "classnames": "^2.2.6",
    "css-loader": "2.1.1",
    "dotenv-expand": "4.2.0",
    "eslint": "^6.1.0",
    "eslint-config-react-app": "^5.0.1",
    "eslint-loader": "2.2.1",
    "eslint-plugin-flowtype": "3.13.0",
    "eslint-plugin-import": "2.18.2",
    "eslint-plugin-jsx-a11y": "6.2.3",
    "eslint-plugin-react": "7.14.3",
    "eslint-plugin-react-hooks": "^1.6.1",
    "file-loader": "3.0.1",
    "font-awesome": "^4.7.0",
    "fs-extra": "7.0.1",
    "html-webpack-plugin": "4.0.0-beta.5",
    "identity-obj-proxy": "3.0.0",
    "is-wsl": "^1.1.0",
    "jest": "24.8.0",
    "jest-environment-jsdom-fourteen": "0.1.0",
    "jest-resolve": "24.8.0",
    "jest-watch-typeahead": "0.3.1",
    "jwt-decode": "^2.2.0",
    "mini-css-extract-plugin": "0.5.0",
    "node-sass": "^4.12.0",
    "optimize-css-assets-webpack-plugin": "5.0.3",
    "parallax-js": "^3.1.0",
    "pnp-webpack-plugin": "1.5.0",
    "postcss-flexbugs-fixes": "4.1.0",
    "postcss-loader": "3.0.0",
    "postcss-normalize": "7.0.1",
    "postcss-preset-env": "6.7.0",
    "postcss-safe-parser": "4.0.1",
    "raphael": "^2.3.0",
    "react": "^16.9.0",
    "react-addons-css-transition-group": "^15.6.2",
    "react-addons-transition-group": "^15.6.2",
    "react-app-polyfill": "^1.0.2",
    "react-chartjs-2": "^2.7.6",
    "react-dev-utils": "^9.0.3",
    "react-dom": "^16.9.0",
    "react-moment": "^0.9.6",
    "react-notifications-component": "^2.1.0",
    "react-raphael": "^0.9.0",
    "react-redux": "^7.1.1",
    "react-router-dom": "^5.0.1",
    "react-transition-group": "^4.3.0",
    "redis-commander": "^0.6.6",
    "redux": "^4.0.4",
    "redux-thunk": "^2.3.0",
    "resolve": "1.12.0",
    "resolve-url-loader": "3.1.0",
    "sass-loader": "7.2.0",
    "semver": "6.3.0",
    "socket.io-client": "^2.2.0",
    "style-loader": "1.0.0",
    "styled-components": "^4.4.0",
    "terser-webpack-plugin": "1.4.1",
    "ts-pnp": "1.1.2",
    "url-loader": "2.1.0",
    "webpack": "4.39.1",
    "webpack-dev-server": "3.2.1",
    "webpack-manifest-plugin": "2.0.4",
    "workbox-webpack-plugin": "4.3.1"
  },
  "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js",
    "build-prod": "set PUBLIC_URL=production11 && node scripts/start.js",
    "build1": "set APP_ENV=development webpack"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "jest": {
    "roots": [
      "<rootDir>/src"
    ],
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts"
    ],
    "setupFiles": [
      "react-app-polyfill/jsdom"
    ],
    "setupFilesAfterEnv": [],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
      "<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
    ],
    "testEnvironment": "jest-environment-jsdom-fourteen",
    "transform": {
      "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
      "^.+\\.module\\.(css|sass|scss)$"
    ],
    "modulePaths": [],
    "moduleNameMapper": {
      "^react-native$": "react-native-web",
      "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "web.ts",
      "ts",
      "web.tsx",
      "tsx",
      "json",
      "web.jsx",
      "jsx",
      "node"
    ],
    "watchPlugins": [
      "jest-watch-typeahead/filename",
      "jest-watch-typeahead/testname"
    ]
  },
  "babel": {
    "presets": [
      "react-app"
    ]
  },
  "devDependencies": {
    "dotenv": "^6.2.0",
    "react-app-env": "^1.2.3"
  },
  "proxy": "http://45.86.181.235" // here
}
Ad

Answer

You can keep environment variables in .env file for different environments

.env file

REACT_APP_CONFIG_URL=http://45.86.181.235

And you can access the value in react like this:

const configUrl = process.env.REACT_APP_CONFIG_URL

And if you want to change by script, In windows:

"REACT_APP_CONFIG_URL=45.86.181.235" && npm start

In linux or macOs:

REACT_APP_CONFIG_URL=45.86.181.235 npm start

More info: https://create-react-app.dev/docs/adding-custom-environment-variables/

Ad
source: stackoverflow.com
Ad