react-scripts Invalid Host header - reactjs

I created an app with react-create-app, I just dev it using npm start, that seems to do react-scripts start according to my package.json
Whenever I add a proxy to my package.json, I get this error message :
Invalid Host header
I get the idea, it's a security issue. What I don't get is how to fix it. I read several issues on github and QA here on the subject, the fix is easy enough, but I still don't get where to put it
in the end, I will add a whitelist of hosts. I think I saw it's possible.
but where do I put this config to start :
devServer: {
disableHostCheck: true
}

Another way to disable the host check would be to set the following environment variable: DANGEROUSLY_DISABLE_HOST_CHECK=true
That can be done by e.g. adding that line to .env file in the root of the project. Note that this is not a secure solution and should not be used in production.

You can manage it without changing stuff inside node_modules or by ejecting your project by using an npm package called react-app-rewired.
It basically has an option to override your default hardcoded settings for webpack that are inside a create-react-app boilerplate setup.
You put a config-overrides.js inside your root folder and change the scripts inside your package.json to match react-app-rewired instead of the react-scripts. This way you can override all the webpack config that's hard coded inside a react project by writing it down inside a config-overrides.js file.
The syntax is inside this link. There's also an article about it which can be found here.

I never found out where to put the webpack.config.js. It didn't work in the app root directory where I suppose it should go, it didn't do anything for me, I just ended up modifying where react-scripts invokes webpack-dev-server and then put the disableHostCheck to true directly before invoking.
Basically I changed the following line :
const serverConfig = createDevServerConfig(
proxyConfig,
urls.lanUrlForConfig
);
to :
var serverConfig = createDevServerConfig(
proxyConfig,
urls.lanUrlForConfig
);
serverConfig.disableHostCheck = true;
that's really not good practice (modify the code and disableHostCheck), but now I know I can actually modify settings, I'll just go for a whitelist, may be one day I'll understand why it doesn't care about my webpack.config.js ^^

Install react-app-rewired:
npm install react-app-rewired --save-dev
change package.json script to
"start": "set PORT=80&&react-app-rewired start",
then add a file named .env.development, add this line:
HOST=buzzbuzzenglish.com
then add config-overrides.js file (you can override some webpack settings there but don't have to - still, file have to be created)
finally you type npm start, then browser will open and navigate to buzzbuzzenglish.com and renders normally without the Invalid Host Header error.

Related

react app doesn't update and .env file doesn't work

I'm in WSL2, and my react app does not update any changes at all, only updates when re-running "npm start"
I've tried "npm install react-dotenv" and creating an .env file with
FAST_REFRESH=false
CHOKIDAR_USEPOLLING=true
doesn't work
tried in the package.json
"start": "CHOKIDAR_USEPOLLING=true react-scripts start"
doesn't work
any suggestions? I don't even mind manually refreshing the browser, it's just that it won't update unless I restart the whole thing.
You do not have to install an additional dotenv package since Create-React-App already supports environment variables natively. However if you use environment variables, you need to prefix them with REACT_APP. e. g. REACT_APP_MY_VARIABLE.
Also note: Whenever you update an environment variable you have to restart the app.
Take a look at the official CRA docs.
Now for the reloading problem. There are a couple of possible solutions:
Add a .env file to your project without third party package and
define a variable named FAST_REFRESH=false. (CRA advanced
configuration)
If you are using a Virtual Machine try adding CHOKIDAR_USEPOLLING=true to your .env file.
There is another common problem in CRA ^17.0.1 with hot reloading (Github issue - Hot Reload stopped working with React "^17.0.1")
if (module.hot) module.hot.accept();
Finally (and this is the most likely solution in my opinion) try to move your project folder to somewhere, where npm can automatically recompile in WSL. E. g. move project from your desktop to your actual home directory.

Adding an .env file to React Project not working

I am trying to add .env file and variables but I am unable to access any variable. I am using React Biolerplate Code.
I am following this React Docs File.
I have added one .env file in my root folder like this:
REACT_APP_SECRET_NAME=secretvaluehere123
And I am trying to access this using this code:
<small>You are running this application in <b>{process.env.NODE_ENV}</b> mode.</small>
I am getting NODE_ENV as development but when I am trying to access:
REACT_APP_SECRET_NAME
I can't access it.
Mine react boilerplate is using:
cross-env NODE_ENV=development
in the start command.
I removed (cross-env NODE_ENV=development) from package.json but it is not working. I tried solutions from this answer: Possible answer.
According to React Docs it should work. I want to add api_url for local it should be x and for the production, it should be y:
The following issue from React Boilerplate seems to suggest a working solution:
Install env-cmd, a node module
Update your start script to use it:
{
start: "cross-env NODE_ENV=development env-cmd node server"
}
This should work if your .env is in the root folder as you've said.
Otherwise, you can specify the path of .env by doing so
{
start: "cross-env NODE_ENV=development env-cmd -f ./custom/path/.env node server"
}
I assume you are trying to access your .env variables inside index.html, if so then syntax is a bit different that in render function. Try to access it like this.
%REACT_APP_SECRET_NAME%
It looks like you're looking at two types of React starters:
React Boilerplate
Create React App
These aren't the same. I don't know React Boilerplate, but I'm not sure if they provide the same environment variables strategy with .env files as Create React App does.
Maybe have a look at the dotenv Node package and perhaps you can add that to your project. I think (not 100% sure) that Create React App uses the same one.
Seems like you have everything right. Just remember to restart your development server every time you change information in your .env file.
Also, your .env file needs to be in the root directory of your react app. You can find all this information in the React Docs - Adding Development Environment Variables In .env

webpack -p not running on create-react-app

I have a big project which I was trying to reduce in size using webpack-p according to here: https://hackernoon.com/reduce-webpack-bundle-size-for-production-880bb6b2c72f
I could not run it as I was encountering problems when running webpack -p (it threw an error on every function of index.js
I thought it might be something with my packages. I decided to create new create-react-app and run the command there. To my surprise, there I get the same error:
What might be the problem here?
If your app uses webpack v4 you must use webpack --mode=production instead of webpack -p.
Possible values for mode are: none, development or production.
Another usage:
// webpack.config.js
module.exports = {
...
mode: 'production',
...
}
You don't have a config file because the command you are trying to run is global, it is not getting from your current working directory. Run npm run build instead, which does that under the hood, but has a lot of other things created by the contributors of create-react-app.
You could pass a config file for the global webpack cli, but you don't have access for that in your folder since it is "not" available for your. npm run build also already applies a lot of optimizations for you.

create-react-app is not using my eslint configuration file

I am using create-react-app to start a new react project, and I want to use my own eslint configuration file, extending from the react-app one.
It seems like my .eslintrc file is not used at all, even though I placed it in the root folder. In my output terminal I can see the file is loaded from the node_modules folder:
.../node_modules/eslint/lib/api.js
Am I missing something, or should I save the file somewhere else?
Thanks.
True, those files are not used by create-react-app.
It is still possible to disable ESLint rules using comments in the JavaScript files:
// eslint-disable-next-line react/style-prop-object
For more information see: http://eslint.org/docs/user-guide/configuring.html#configuring-rules
According to the documentation, now it is available by setting the EXTEND_ESLINT env variable. But the feature is experimental right now.
See the Experimental: Extending the ESLint config section.

Couldn't find preset "airbnb" relative to directory

I'm trying to set up Enzyme (written by Airbnb) in order to run some UI tests in React. However, no matter what configuration I have (and I've tried several) I keep getting this error. I added a .babelrc file which has the following inside:
{
"presets": ["airbnb"]
}
but it still gives me the same error. The .babelrc file is on the same level as the package.json file. I've searched everywhere online to see what could be causing this, but I'm at a loss. Any ideas?
make sure to install the preset
npm install --save-dev babel-preset-airbnb

Resources