score:2
a common solution is to check against process.env.node_env
like this:
const hostname = process.env.node_env === "development" ? "localhost:9876" : "localhost:6789";
you may need to force the environment variable to be present in your webpack configuration file using the defineplugin
like this:
plugins: [
new webpack.defineplugin({
"process.env": {
node_env:json.stringify(process.env.node_env || "development")
}
})
]
an alternative solution might be to use the config package and provide your hostname string as a configuration parameter. config will inherit configurations from files based on the current node_env. it will start by using configuration from a default.json file, then override it with a development.json, or production.json, depending on your environment.
note that you'll need for the config files to be copied to your output directory using copywebpackplugin for it to work.
score:0
there are definitely many ways you could achieve that. one of those solutions would be to use webpacks's defineplugin
. in your plugins section in webpack configuration you would add something like this:
new webpack.defineplugin({
api_host: process.env.node_env === 'production'
? json.stringify('localhost:8080')
: json.stringify('api.com')
}),
that creates a global variable api_host
available everywhere in your codebase, which you can use. you can read more about the defineplugin
here https://webpack.js.org/plugins/define-plugin/
score:0
you could use a relative path when you make any request to your api server instead of calling the full url for your app.
and for your development you could add a proxy property to your package.json file:
{
...
"proxy": {
"/api/*": {
"target" :"http://localhost:9876"
}
}
}
so whenever you make any request that prefixed with /api
will be redirected to http://localhost:9876/api
this is just in the development but in the production if you make a request prefixed with /api
it won't be redirected it will be served normally because the proxy is just available in the dev server coming with create-react-app
.
Source: stackoverflow.com
Related Query
- How to parameterize variables in a React Webpack app for different environments?
- How to support react js app for 2 different sub domains
- Externalize different environment variables for different environments in React
- How to deploy a React App for each PR in a different URL on Azure?
- How to make build for react app for different stages?
- How can I create multiple instances of a React component (having an infinite animation) with different values for the :root variables
- How to use static js generated throught webpack for react app with es6
- How to update webpack config for a react project created using create-react-app?
- How to use environment variables in React app hosted in Azure
- How to set headers for a React app created using 'create-react-app'
- How do I configure my .htaccess file for React App in Subdirectory?
- how to share environment variables between react app and express js server hosting it as static site
- How to change default localhost port for React App
- React app is looking for static files in different location when using proxy
- how to make webpack typescript react webpack-dev-server configuration for auto building and reloading page
- How to change PublicPath for Create React App in Dev Environment
- How to implement a React infinite scroll component for a chat app like Facebook Messenger?
- How to Fix Heroku Application Error For a Successfully Deployed React App
- How to access environment variables in react from docker and webpack
- How to deploy a react app to mutiple environments without multiple builds?
- How to use a different delay for each item with React transition group?
- How to reduce react app build time and understanding behaviour of webpack when bundling
- how to set font size for different IOS devices in react native
- How to handle multiple environments in React app
- React how to print landscape / portrait for different components
- How to use Turborepo for an existing react app created with Create React App in order to make it a monorepo?
- How can I programmatically set the user details for Launch Darkly in my react app
- How to apply react-native-linear-gradient for the entire app background with React Native
- How to get Font Awesome Icon in react app for a pseudo element in css
- How to have different style for items at certain locations in a list in React Native?
More Query from same tag
- How to automatically expand the selection to the entire linked text in Draft.js?
- Setting a backgroundImage With React Inline Styles
- How to use material-ui inputBase to make select component?
- Sending form data with react Router from a Component to another
- react does not update localstorage value based on state
- Invariant Violation: Query(...): Nothing was returned from render. This usually means a return statement is missing
- React, delete component with a button inside that component
- Jsx does not show props properly
- Material-ui adding Link to Radio Button component from react-router
- React router 4 - routing + code splitting, with webpack 1.9
- I want to find the path of the folder
- How do I pass an array modifier as a prop?
- Typescript how can I return an HTML element multiple times with a loop?
- How to display an image or multiple images from an array of object to react frontend cart section?
- How To Stop React Rerendering My Page With Dynamic Imports?
- Options for dynamically adding reducers within a package being consumed
- State variables not updating and I so not know why ReactJS
- How can I mutation material-ui Input without save button?
- Stop Webcam in React Component in ComponentWillUnmount
- How to create a dropdown list from a json object?
- Cannot deploy ASP.NET MVC + React to Azure
- react on datepicker click want to change toggle value
- Divider Disappears When Adding Floated Tag on Vertical Menu Semantic UI React
- Send a value via EmailJs without using a form
- CORS policy issue on Heroku Server
- How do I add css variable into a reactJS component that is written with es6?
- Polyline not rendering on react-google-map
- How to filter and sort the same array of object state in redux?
- Defining React Prop Keys When Calling Static Child Components
- Javascript/react - highlight matching parenthesis