I have a React app that I am deploying with Heroku. It only has a frontend and no backend.
In my app, I have 3 environment variables for EmailJS: USER_ID, SERVICE_ID, TEMPLATE_ID.
Here are the places I use them:
index.html
<script type="text/javascript">
(function(){
emailjs.init("%USER_ID%");
})();
</script>
email.js
emailjs
.sendForm(
process.env.SERVICE_ID,
process.env.TEMPLATE_ID,
e.target,
process.env.USER_ID
)
I have read the documentation on heroku here which says I need to use the heroku CLI or the config variables setting in the heroku dashboard.
I have a .env file that I have for local development with all these variables defined. The variables are also undefined when I try locally with npm start.
When I use bash and type echo $SERVICE_ID I get the desired outcome.
I have tried both heroku CLI and the config variables setting in the heroku dashboard and the env variables are still undefined.
What might I be doing wrong? Please let me know if I am unclear or if you guys would like more info. Thanks!
package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.9",
"#testing-library/react": "^11.2.3",
"#testing-library/user-event": "^12.6.2",
"axios": "^0.21.1",
"dotenv": "^8.2.0",
"emailjs-com": "^2.6.4",
"jquery": "^3.5.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-ga": "^3.3.0",
"react-scripts": "^4.0.1",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I believe I fixed it.
I changed all of my env variables to start with REACT_APP_ which according to the react documentation, is the proper way to deal with env variables in react. I also used react-dotenv which #Hoobs recommended.
Related
I am a little bit perplexed here as I attempt to launch my first react project online.
I'm using AWS Amplify, I have connected my github repo, and have everything linked together, from VS Code I can push to my github, which then after a minute or so displays the changes inside of my AWS Amplify website, so I believe I have the vscode - GH - amplify pipeline operating properly...
I initialized a base react app, on the AWS site and it works as expected,
The dev server on localhost displays the same spinning logo and
Edit src/App.js and save to reload
I then added some extra random text as a test, within the app.js,
It added my hello world test text in the localhost. I push it to GH, it then appears exactly the same on my Amplify site as it does on dev server. the same react initial page but now both have the test text. I then delete all the components of the app.js
add a simple header nav bar, update package.json to include a couple dependencies (chakra-ui) and then the whole thing breaks.
I'm sorry if this is a dumb question but do I need to do anything outside of vs code, in order to have the package.json updates not ruin the compiling?
In VS Code i need to npm install packages do I need to do this somewhere inside AWS Amplify?
here is the json that works fine
{
"name": "react-amplify-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I then try to add the JSON file from my full react project where i made a decently involved (for me anyway) website.
Here is the updated JSON that makes it not work anymore
{
"homepage": "https://avarga1.github.io/My-Portfolio/",
"name": "myreactportfolio11",
"version": "0.1.0",
"private": true,
"dependencies": {
"#chakra-ui/core": "^0.8.0",
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"iframe-resizer": "^4.2.11",
"react": "^18.2.0",
"react-bootstrap": "^2.7.0",
"react-dom": "^18.2.0",
"react-s3": "^1.3.1",
"react-scripts": "^5.0.1",
"scripts": "^0.1.0",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"gh-pages": "^4.0.0"
}
}
What am I missing???
Thanks in advance.
I've created a reactjs app and I deployed it to github pages with the command npm run deploy this all works great and I can see my app running when i open it from the
settings>pages in my repo. However, I am trying to add a custom domain and this is causing me trouble. I configured the IPs and everything for the DNS according to gh-pages documentation and the DNS checks all pass when i add the domain name. But when i go to the domain in my browser I get all 404 errors like this:
Im thinking it could be something wrong with my package.json, I was thinking I need to change the "homepage" value to my new domain instead of the http://<Github-username>.gihub.io/<repoName>/ I set it to when i deployed the app? Ive tried this but it doesnt work to load the page at that url.
So Im wondering if anyone has run into this issue after deploying to gh-pages and then adding a custom domain?
this is my package.json:
{
"homepage": "http://CRJones7.github.io/learned/",
"name": "learned",
"version": "0.1.0",
"private": true,
"dependencies": {
"#primer/octicons-react": "^17.9.0",
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"bootstrap": "^5.2.3",
"jquery": "^3.6.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.4.4",
"react-scripts": "5.0.1",
"reactstrap": "^9.1.5",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"gh-pages": "^4.0.0"
}
}
I figured out the solution, I just needed to re-deploy my page with
npm run deploy
Having some trouble debugging my react app. This app was created using create-react-app, so I should be able to run 'npm start' and see the app run in the localhost window in my browser, but instead I see nothing, and in the Elements tab of chrome devtools shows a tag saying "You need to enable javascript to run this app".
Not sure what is going on here. I had this app running perfectly fine yesterday, and I didn't change anything to the package.json.
Here's what my package.json looks like:
`
`{
"name": "app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.4.3",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}`
`
Can anybody help me with this? I tried adding a common solution, to add "proxy": "localhost:5000", but that didn't solve the problem.
I was expecting the app to successfully start in the browser.
This is my package.json in react.
{
"name": "reactjs",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"bootstrap": "^4.5.3",
"react": "^17.0.1",
"react-bootstrap": "^1.4.0",
"react-dom": "^17.0.1",
"react-scripts": "4.0.0",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:8080",
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I used proxy in the package.json file but it doesn't work for me. after adding this proxy it didn't show any error but also host in local host3000.
React is regularly front-end framework. This means at the end of the day, It will be built to bunch of js file. Hence react-scripts build. You should deploy those files to a proper HTTP server that can serve static files. Check this out for making HTTP file server out of node https://stackoverflow.com/a/16350826/3849555 . react-scripts start is just starting web server for development-purpose only. Do not use it as real web server.
UPDATE: I found out that the index.html is linking the files incorrectly like it doesn't recognize where the files are stored. I corrected the css link tag in the header of my bundled document but Now I am getting errors with JavaScript still and I cannot see how to fix them because its a very large map.
Whenever I use react-scripts to build out my CRA Application and run the document I get
I am almost 95% sure this is because of react-bootstrap. Just wondering if anyone else is having these issues and if there is a fix?
Here are my dependencies
{
"name": "test-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"bootstrap": "^4.4.1",
"gh-pages": "^2.2.0",
"react": "^16.13.1",
"react-bootstrap": "^1.0.0",
"react-dom": "^16.13.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
"reactstrap": "^8.4.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"deploy": "gh-pages -d build"
},
"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"
]
}
}
Found the answer here:
https://github.com/parcel-bundler/parcel/issues/470#issuecomment-354734896
It turns out you need to specify in your build command with Parcel that there should not be a / in front of the referenced files.
In case that link becomes deleted this what I am referencing:
Sorry i forgot a dot try parcel build index.html --public-url ./
this is also documented on the doc site: https://parceljs.org/production.html