I deployed my react app on heroku and chose github as deployment method. To hide the source code, I added GENERATE_SOURCEMAP=false in a .env but it didn't work. In package.json also I tried adding
"build": "set \"GENERATE_SOURCEMAP=false\" && react-scripts build" but it didn't work. I also added it in Config Vars in the heroku project settings.
Please help!
If you are using linux adding it to the package.json will do the trick:
"scripts": {
...
"build": "GENERATE_SOURCEMAP=false react-scripts build"
},
Related
I would like to run a vite react app locally in production mode? How can I do it?
In the vite docs I did get any solution for that. Any hint about the solution would be appreciated.
Go to package.json file of you vite react app.
modify your script like that :
{
"scripts": {
"build": "vite build",
"serve": "vite preview"
}
}
then run :
npm run build
npm run serve
I am currently working on a NextJS Project that has an component. On my local environment everything works fine, when the viewport width changes the iFrame reacts changing the styles for the rendered page inside it. After deployment on Vercel Rendering the same component doesn't work. Styles are not causing any sort of reaction on the component rendered inside the iFrame, not even media queries. Any idea on why this might be happening?
Thanks
I got similar issue before and it's because the deployment environment on production, just install cross-env package and change the build script in your package.json file like this :
"scripts": {
"dev": "cross-env NEXT_PUBLIC_FRAMEWORK=next NODE_ENV=development node server.js",
"build-next": "cross-env NEXT_PUBLIC_FRAMEWORK=next next build",
"build": "yarn build-next",
"start": "node server.js -p 80",
"build:staging": "env-cmd -f .env.staging yarn build",
"start-next": "cross-env NEXT_PUBLIC_FRAMEWORK=next NODE_ENV=production node server.js"
},
After creating a new web app with create-react-app (CRA), I need to include some environment files for configuring various endpoints. Noticed that CRA comes with the cool dotenv package all ready to go. There's only one problem with that - I would like to have dotenv read these files from within my ./environments directory and not the root directory. Is there any way to load the .env, .env.local, .env.test, etc... files in a directory separate from the root directory?
Noticing I can achieve this in my express backend server.js by simply importing like so:
require('dotenv').config({ path: `./environments/` })
Can I do the same with my client-side code in React? If so, where should I put this import? Doesn't seem to work for me.
If you created your project using CRA and want to configure dotenv to change the path from where it loads the env files, you will have to do npm eject.
Alternatively, you can use env-cmd to achieve the same:
Installation:
npm i env-cmd
Add scripts in package.json, for example:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"dev": "env-cmd -f envs/.env.dev react-scripts build",
"qa": "env-cmd -f envs/.env.qa react-scripts build",
"demo": "env-cmd -f envs/.env.demo react-scripts build"
},
After creating .env.dev, .env.qa, .env.demo in envs/ directory.
Now, you can run:
$ npm run dev // it will use envs/.env.dev file
$ npm run qa // it will use envs/.env.qa file
$ npm run demo // it will use envs/.env.demo file
npm run build creates production build of the project. How do I create development build? I'm using gradle-node-plugin to build the artifact. The project has standard create-react-app configuration.
This is not really doable with just create-react-app, there is an open issue Here and it doesn't look like it will be added anytime soon.
However, you can use a package called dotenv for that, following are the steps you should take:
Install dotenv (make sure to add save dev) npm install dotenv-cli --save-dev
In package.json scripts section add new script: "build:dev": "dotenv -e .env.development react-scripts build",
And you can build for development with npm run build:dev
PS: if you want to avoid mistakenly deploying dev builds to production (as mentioned here) you can add build:prod to package.json and disable the regular build command, see code:
"build:dev": "dotenv -e .env.development react-scripts build",
"build:prod": "dotenv -e .env.production react-scripts build",
"build": "echo \"Please use build:dev or build:prod \" && exit 1",
Also note that process.env.NODE_ENV will still be production but it'll load your .env.development file
Thanks, #Moses. This is an extension to the answer posted by Moses Schwartz. You can also make the build pick the environment files dynamically by exporting the value of the environment(development, test, production) in the bash shell. And then you don't have to have different build commands for different environments.
You can use this in your package.json
"start": "dotenv -e .env react-scripts start",
"build": "dotenv -e .env.${REACT_APP_ENVIRONMENT} react-scripts build",
So when your run npm start, it will pick the environment values from .env
and when you run npm run build, it will pick the environment values from .env.{REACT_APP_ENVIRONMENT}
To define the REACT_APP_ENVIRONMENT, you can do:
export REACT_APP_ENVIRONMENT="development" or
export REACT_APP_ENVIRONMENT="test" or
export REACT_APP_ENVIRONMENT="production"
Hope this helps. This will help in staging the react application for multiple environments.
Thanks to #Tx_monster comment
github.com/Nargonath/cra-build-watch
A script for create-react-app that writes development builds to the disk
npm install -D cra-build-watch
package.json:
{
"scripts": {
"watch": "cra-build-watch"
}
}
npm run watch
How to use .env in react . I can get access to .env.development and .env.production with
"start":"react-scripts start",
"build": "react-scripts build",
How to get access to another like .env.staging ?
I gave like this
"build_staging": "set REACT_APP_ENV=staging & react-scripts build",
but not working.
Any suggestions please.
To keep things consistent across linux(my production server) and windows(my development server) I use cross-env
npm install --save cross-env
and my scripts look like this
"scripts": {
"dev": "cross-env NODE_ENV=development node server",
"build": "cross-env NODE_ENV=production next build ",
"start": "cross-env NODE_ENV=production node server"
},
so to set a custom env like REACT_APP_ENV you'll need to
"build_staging": "cross-env REACT_APP_ENV=staging react-scripts build",
and you can access it in your javascript code using
process.env.REACT_APP_ENV
also to start a staging server you might want to add
"start_staging": "cross-env REACT_APP_ENV=staging react-scripts start"
more about this here
[CONTEXT]
- I've created React project with Create React.
- Running on Windows 10
- Using VSCode IDE
- I have the file .env.development on above /src folder.
- My code has console.log(process.NODE_ENV) and console.log(process.REACT_APP_YOUR_KEY)
[PROBLEM]
When I'm running the program with npm start, the browser prints 'development' for NODE_ENV, but for my React .env variable, it prints undefined.
I try to run npm start with changing the default script of package.json to this start script: set REACT_APP_YOUR_KEY && react-scripts start. Then there isn't any undefined, all works well. 🤨
[SOLUTION]
The cause was that the file .env.development is not detected correctly. My hypothesis is the environment development file, Windows or VSCode don't detect well.
Windows File Explorer files
VS Code explorer. Look up the icon of the files 🤔
You have to change the file name from .env.development to .env.
[SOLUTION]: I've created env.js as shown below.
env.js placement
after that i have added in index.html the script below
my env.js content
NB: i can acces my env.js var without import them (it seems the best way for me)