Why my React App does not fire up in browser automatically? - node.js

I am learning React now. I create the app by using
npx create-react-app .
npm start
Then I get:
Compiled successfully!
You can now view recipe_app in the browser.
Local: http://localhost:3000
On Your Network: http://192.168.0.11:3000
Note that the development build is not optimized.
To create a production build, use npm run build.
The compilation works fine, but the application does not open in my browser (I use firefox.) automatically, so I have to manually open it by typing the URL in my browser. I thought React should fire up the app by default? I am not sure what is wrong. I tried the following solutions but none of them work.
Use BROWSER=firefox npm start in my terminal.
Set BROWSER variable in package.json:
// package.json
"scripts": {
"start": "BROWSER='firefox' react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
or add cross-env in the scripts
// package.json
"scripts": {
"start": "cross-env BROWSER='firefox' react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
None of these solutions work. The application can be compiled successfully but it just does not open up in my browser. Here is my full package.json file:
{
"name": "recipe_app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.12.0",
"#testing-library/react": "^11.2.6",
"#testing-library/user-event": "^12.8.3",
"cross-env": "^7.0.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.1"
},
"scripts": {
"start": "BROWSER=firefox react-scripts start",
"chrome": "BROWSER='google-chrome' 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 am using vscode 1.55.2, firefox 88.0, npm 6.14.11, node v14.15.1, and Ubuntu 20.04.2 LTS. Can anyone help me? Thank you!

I figured it out. It is because $DISPLAY is not in the environment variables of the vscode's integrated terminal.
In order to fire up firefox, I need to set export DISPLAY=:0. But this is a one-time fix.
I do have $DISPLAY in my zsh terminal, clearly, vscode does not inherit it. So I need to set the inheritEnv to be true in the setting.json file.
Open ~/.config/Code/User/settings.json, and set:
"terminal.integrated.inheritEnv": true
Then re-start vscode. To ensure your modification works, just type in your terminal:
echo $DISPLAY
:0 // you get this
You are good to go.

Related

Heroku deployment failing: Failed to load plugin 'cypress' declared in 'package.json': Cannot find module 'eslint-plugin-cypress'

Update #2 after some more research
Looking into the build logs some more, it looks like Cypress gets installed via a postinstall hook that downloads a binary that Heroku doesn't pick up in the build process for some reason unless config is set to NPM_CONFIG_PRODUCTION=false. Comparing the failed build log with the successful build log, this postinstall hook doesn't run:
> cypress#6.0.0 postinstall /tmp/build_0453cc7d/frontend/node_modules/cypress
Why this and potentially other devDependencies don't get installed prior to npm run build doesn't appear to be documented and runs contrary to Heroku's documentation that devDependencies are installed by default.
Update #1 with more build info
Here's a gist that shows the failing build log and the succeeding build log (after configuring Heroku not to prune devDependencies using heroku config:set NPM_CONFIG_PRODUCTION=false).
https://gist.github.com/ddehart/9e0ca72a3f20f104e05d70eed6de6c64
The pruning appears to be initiated after the build process installed Cypress, so it's not clear why setting NPM_CONFIG_PRODUCTION=false prompted the Cypress installation, despite Heroku's documentation that says:
By default, Heroku will install all dependencies listed in package.json under dependencies and devDependencies.
Original question
I'm getting this npm run build error when deploying to Heroku:
Failed to load plugin 'cypress' declared in 'package.json': Cannot find module 'eslint-plugin-cypress'
Here's my package.json for reference:
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.6",
"#testing-library/react": "^11.2.2",
"#testing-library/user-event": "^12.2.2",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "^4.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"cypress:open": "cypress open",
"cypress:run": "cypress run"
},
"eslintConfig": {
"extends": [
"react-app",
"plugin:cypress/recommended"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#testing-library/cypress": "^7.0.2",
"cypress": "^6.0.0",
"eslint-plugin-cypress": "^2.11.2"
}
}
The project was initiated using Create React App. npm run build succeeds without any issues locally, so I first thought maybe Heroku was pruning devDependencies, but looking back at other build logs that succeeded show that pruning happens after a successful build.
If it helps, here's my package.json from the last successful build.
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"react-scripts": "3.4.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"cypress:open": "cypress open"
},
"eslintConfig": {
"extends": [
"react-app",
"plugin:cypress/recommended"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"cypress": "^5.4.0",
"eslint-plugin-cypress": "^2.11.2"
}
}
I think the build is complaining about this, although that hasn't changed at all from the last successful build.
"eslintConfig": {
"extends": [
"react-app",
"plugin:cypress/recommended"
]
},
Removing that plugin declaration of course ruins Cypress in my IDE. Is there any way to work around the build error or troubleshoot on the Heroku instance itself?
Rather than setting NPM_CONFIG_PRODUCTION=false, I ended up moving the Cypress eslintConfig into its own .eslintrc.json file within the cypress directory. Since Heroku only seems to be getting hung up on the Cypress plugin referenced in the main package.json, removing that configuration altogether fixed the Heroku build issue without affecting my IDE and without having to rely on the non-standard NPM_CONFIG_PRODUCTION setting.
Per Cypress's documentation, the .eslintrc.json file just needs this for the recommended rules:
{
"extends": [
"plugin:cypress/recommended"
]
}

Could not complete "npm run deploy" command for React to use on Github Pages

I'm trying to deploy a website on Github pages, and the project was created using create-react-app and has gh-pages module installed. All of my local and remote repositories are up to date, and using npm start launches the page on local host with no issues.
However when I try to deploy this site to Github Pages using npm run deploy, it compiles successfully but then hangs forever on the gh-pages -d build command. It goes on forever, and does not exit out of it by itself with an error message nor is an error log created. I suppose the command could just be taking really long to finish, but I waited for 30 minutes without it ending so I am not sure.
Below is my package.json:
{
"name": "website",
"version": "0.1.0",
"private": true,
"homepage": "https://xxx.github.io/website",
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.4.0",
"#testing-library/user-event": "^7.2.1",
"gh-pages": "^2.2.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.3.0"
},
"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"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
here is my shell log after force termination of npm run deploy:
> website#0.1.0 predeploy C:\Users\user\directory
> npm run build
> website#0.1.0 build C:\Users\user\directory
> react-scripts build
Creating an optimized production build...
Compiled successfully.
File sizes after gzip:
40.32 KB build\static\js\2.08db1231.chunk.js
2.9 KB build\static\js\main.0c674757.chunk.js
784 B build\static\js\runtime-main.e77b87bb.js
547 B build\static\css\main.d1b05096.chunk.css
The project was built assuming it is hosted at /website/.
You can control this with the homepage field in your package.json.
The build folder is ready to be deployed.
To publish it at https://xxx.github.io/website , run:
npm run deploy
Find out more about deployment here:
bit.ly/CRA-deploy
> website#0.1.0 deploy C:\Users\user\directory
> gh-pages -d build
as an aside does my github free account status have to do with anything?
Use version 2.0.1 instead of 2.2.0 for gh-pages
running "gh-pages -d public" hangs on "pushing" per node debug #324

how to resolve "EADDRINUSE" error and "Proxy error"?

I have two problems when running my NPM. The first problem is whenever I save my files. I get this error message.
Also when I make a request to the server from my frontend. I get the proxy error message
This is my scripts in my package.json for my backend
"scripts": {
"client": "npm start --prefix client",
"server": "node app.js --ignore client",
"start": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"test": "echo \"Error: no test specified\" && exit 1"
},
And also my package.json for my client as you can see below
{
"name": "client",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:5000",
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.8",
"#fortawesome/free-brands-svg-icons": "^5.6.3",
"#fortawesome/free-solid-svg-icons": "^5.5.0",
"#fortawesome/react-fontawesome": "^0.1.3",
"axios": "^0.19.0",
"react": "^16.10.2",
"react-dom": "^16.10.2",
"react-router-dom": "^5.1.2",
"react-scripts": "3.2.0",
"react-spring": "^8.0.27"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"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"
]
},
"devDependencies": {
"node-sass": "^4.12.0"
}
}
Assuming you're on windows:
Run netstat -aon to view the processes running on each port
Find the process running on 0.0.0.0:5000 or 127.0.0.1:5000 and note its PID
Run taskkill /F /PID <pid_number> to kill that process
You should now be able to run your npm service on that port
If you're on Linux or Mac you should be able to follow the same process, just google what the commands are called on those OSs
EADDRINUSE tells you, port 5000 is already open by another process or the same process. So either something else is running on the port, or in your case it is more likely your app doesn't close the port properly when restarting.
You can manually force close the process and restart it, but that can be quite a pain, instead of restarting when you save. Since you probably want the latter.
So I'd suggest looking at your code and check for things that could prevent your app from shutting down, or delay your shut down. So Pay attention to things such as promises that have not resolved yet, and timeouts that are still running.
As for your proxy issue you get a ECONNRESET, which basically says your connection has been shut down by the peer (so the proxy in your case), most likely because port 5000 gets shutdown and restarted. I reckon that one will be solved once you resolve your first issue.
Hope this helps with resolving your issue.

How can I deploy a react app with a node backend on GitHub Pages?

I just started learning about concurrently npm to React.
I want to know how to deploy React project on Github page with concurrently npm.
Normally on local machine we would run the app with server side package.json file
"dev": "concurrently "npm run server" "npm run client""
In Terminal> npm run dev
I tried with gh-pages npm to deploy react app on Github page but with concurrently npm,
I have no idea how to do it since there are two package.json.
Also, I don't know much about NODE environment and npm run build.
Server side package.json:
{
"name": "contact-keeper",
"version": "1.0.0",
"description": "Contact manager app",
"main": "server.js",
"homepage": "https://myprofile.github.io/Contact-Keeper-with-React",
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"clientinstall": "npm install --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\""
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"config": "^3.1.0",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"express-validator": "^6.1.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.6.4"
},
"devDependencies": {
"concurrently": "^4.1.1",
"gh-pages": "^2.1.1",
"nodemon": "^1.19.1"
}
}
Client side server package.json:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.19.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.1",
"react-scripts": "3.0.1",
"react-transition-group": "^4.2.1",
"uuid": "^3.3.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"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"
]
},
"proxy": "http://localhost:5000"
}
Is there any document or basic knowledge about setting environment that I need to learn more to understand this topic?
Github is not (quite) a service for serving your working application to other users. Github is a repository for storing your code, updating it, and managing versions.
I think that to deploy your app "concurrently" to github means there is a way of updating Github with the new version code, at the same time as you deploy it online to a service, such as Heroku.
You should research and understand what Git (as opposed to github) is, as its essential for development. Get skilled at managing your app code with git first, before trying to deploy to a service like Heroku, AWS etc....
EDIT
As pointed out by Asaf Aviv, you can serve front-end apps from Github, with github pages, but you still need to be able to push your local code up to github for this to work.

React-timestamp issue. Can't resolve 'react' in node modules

I'm creating an App with React and everything has been fine for weeks until I installed react-timestamp with npm in order to convert unix time (https://www.npmjs.com/package/react-timestamp).
But now it won't compile and says:
/Users/nembokid/node_modules/react-timestamp/lib/timestamp.js
Module not found: Can't resolve 'react' in '/Users/nembokid/node_modules/react-timestamp/lib'
I've tried to remove package-lock.json as well as yarn.lock, then did npm install again followed by npm run start, but still same issue. Also tried to add it to my package.json file, but without success.
In '/Users/nembokid/node_modules/react-timestamp/lib' there's only one file: timestamp.js. Should it also contain a file called react.js?
How can I get around this? Should I try to delete react-timestamp from my node_modules folder? Don't really understand the issue here. Would be grateful for all help!
Edit: package.json:
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react-router-dom": "4.3.1",
"react-scripts": "2.1.1",
"web3": "^1.0.0-beta.37"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
I deleted the folder /Users/nembokid/node_modules/react-timestamp and then from the terminal, inside the project client folder: npm run start without npm install or anything. Works perfectly fine again.

Resources