I am trying to deploy full stack MERN app from one github repository on render.com. After deploying, my front end and back end work individullay fine but they don't work together. For examle, if I send GET request from Postman to deployed back-end I get data as a response but from front-end static site I don't get any response.
I created Static Site and Web Service connecting a single github repo that contain my front-end and back-end. When deploying back-end console show this error
This is my file structure:
Render Web Service setup:
Name: support-desk-api
Branch: main
Root Directory: e.g. src (Default)
Build Command: yarn (Default)
Start Command: npm run dev
Render Static Site setup:
Name: support-desk
Branch: main
Root Directory: e.g. src (Default)
Build Command: npm run render-postbuild
Publish directory: ./client/build
package.json of root
{
"name": "support-desk",
"version": "1.0.0",
"description": "support ticket app",
"main": "server.js",
"scripts": {
"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\"",
"render-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"author": "Arifur Rahaman",
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"colors": "^1.4.0",
"concurrently": "^7.5.0",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-async-handler": "^1.2.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^6.3.4"
},
"devDependencies": {
"nodemon": "^2.0.16"
}
}
package.json of client
{
"name": "frontend",
"version": "0.1.0",
"proxy": "http://localhost:5000",
"private": true,
"dependencies": {
"#emotion/react": "^11.9.0",
"#emotion/styled": "^11.8.1",
"#mui/icons-material": "^5.8.4",
"#mui/material": "^5.8.2",
"#reduxjs/toolkit": "^1.8.2",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^14.2.0",
"axios": "^0.27.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-icons": "^4.4.0",
"react-modal": "^3.15.1",
"react-redux": "^8.0.2",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"react-toastify": "^9.0.3",
"toastify": "^2.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"
]
}
}
server.js
const express = require('express')
const dotenv = require('dotenv').config()
const { errorHandler } = require('./middleware/errorMiddleware')
const connectDB = require('./config/db')
const PORT = process.env.PORT || 5000;
connectDB()
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use('/api/users', require('./routes/userRoutes'))
app.use('/api/tickets', require('./routes/ticketRoutes'))
//* Serve static assets in production, must be at this location of this file
if (process.env.NODE_ENV === 'production') {
//*Set static folder
app.use(express.static('client/build'));
app.get('*', (req,res) => res.sendFile(path.resolve(__dirname, 'client', 'build','index.html')));
}
app.use(errorHandler)
app.listen(PORT, () => { console.log(`Server is running at ${PORT}`) })
Any help would be appreciated
Related
My website (react app) returns a blank page after deploying on Heroku. Although, it runs well on localhost. Hence, it most likely has to do with how Heroku run build and/or start.
The error arose after I had to change the scripts in package.json to :
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
},
I had to change from react-scripts to react-app-rewired as it was a requirement to use some libraries (e.g. most web3-related libraries).
Below are my package.json and server.js below. I read others having issues with server.js but it was working fine in my case before I made the changes mentioned above.
Package.json
{
"name": "app",
"version": "0.1.0",
"engines": {
"node": "16.16.0",
"npm": "8.11.0"
},
"private": true,
"dependencies": {
"#appbaseio/reactivesearch": "^3.34.3",
"#biconomy/web3-auth": "^1.0.0",
"#headlessui/react": "^1.7.5",
"#metamask/onboarding": "^1.0.1",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"apexcharts": "^3.35.3",
"assert": "^2.0.0",
"axios": "^0.27.2",
"bn.js": "^5.2.1",
"bootstrap": "^4.6.0",
"browserify": "^17.0.0",
"caver-js": "^1.8.2",
"chart.js": "^3.8.0",
"elliptic": "^6.5.4",
"ethereum-unit-converter": "^0.0.17",
"ethereumjs-util": "^7.1.5",
"ethers": "^5.7.0",
"express": "^4.18.2",
"js-sha3": "^0.8.0",
"moralis": "^1.11.0",
"primeicons": "^5.0.0",
"primereact": "^8.1.1",
"react": "^18.2.0",
"react-apexcharts": "^1.4.0",
"react-app-rewired": "^2.2.1",
"react-card-flip": "^1.1.5",
"react-device-detect": "^2.2.2",
"react-dom": "^18.2.0",
"react-form-stepper": "^2.0.3",
"react-ga": "^3.3.1",
"react-icons": "^4.6.0",
"react-minimal-pie-chart": "^8.3.0",
"react-moralis": "^1.4.1",
"react-router-dom": "^6.4.3",
"react-scripts": "5.0.1",
"react-slick": "^0.29.0",
"react-step-wizard": "^5.3.11",
"react-toastify": "^9.0.5",
"slick-carousel": "^1.8.1",
"stream": "^0.0.2",
"swr": "^2.0.0",
"web-vitals": "^2.1.4",
"web3": "^1.7.5",
"web3modal": "^1.9.8"
},
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
},
"proxy": "https://buckets-backend.herokuapp.com",
"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"
]
}
}
server.js
// server.js
const express = require('express');
const compression = require('compression');
const path = require('path');
const app = express();
app.use(compression());
app.use(express.static(path.join(__dirname, 'build')));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`App is running on port ${PORT}`);
});
Procfile
web: node server.js
I am stuck, any idea how I could debug this?
Heroku will call start script automatically. You need to change the current "start" script to
"start:dev": "react-app-rewired start",
then create a "start" script which calls the server.js file of your server.
// make sure you pass a correct path to server.js
// if server.js is inside server folder, server/server.js
"start": "node server.js",
The issue was related to one of the new package I used: web3auth. According to their docs, the following resolved the problem:
Replace in package.json:
"browserslist": {
"production": [
"chrome >= 67",
"edge >= 79",
"firefox >= 68",
"opera >= 54",
"safari >= 14"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
Source: https://web3auth.io/docs/troubleshooting/react-big-int-error
I have an electron react app (built with create-react-app) which runs fine in development mode, but when I use electron-packager to publish it both the .css and .js files have the error "Failed to load resource: net::ERR_FILE_NOT_FOUND".
Looking in the developer tools network tab I can see it is looking for the files at file:///static/js/main.83d4505c.js and file:///static/css/main.00ef8ad8.css while the file/folder structure is:
build
static
js
main.83d4505c.js
css
main.00ef8ad8.css
App-darwin-x64
App (The executable app)
public
src
...
I use the below code for my main window:
mainWindow.loadURL(
isDev
? 'http://localhost:3000'
: `file://${path.join(__dirname, '../build/index.html')}`
);
I have tried adding "homepage": "./", to the package file as suggested on https://github.com/electron/electron/issues/1769 but it doesn't help.
Package.json
{
"name": "app",
"version": "0.1.0",
"private": true,
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"electron-dev": "concurrently \"BROWSER=none yarn start\" \"wait-on http://localhost:3000 && electron .\""
},
"homepage": "./",
"main": "public/electron.js",
"directories": {
"buildResources": "build",
"app": "build"
},
"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"
]
},
"dependencies": {
"#emotion/core": "^11.0.0",
"#emotion/react": "^11.9.3",
"#emotion/styled": "^11.9.3",
"#mui/icons-material": "^5.8.4",
"#mui/material": "^5.8.5",
"#mui/styled-engine-sc": "^5.8.0",
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^13.0.0",
"#testing-library/user-event": "^13.2.1",
"chokidar": "^3.5.3",
"electron-is-dev": "^2.0.0",
"electron-reload": "^2.0.0-alpha.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"styled-components": "^5.3.5",
"web-vitals": "^2.1.0"
},
"devDependencies": {
"concurrently": "^7.2.2",
"electron": "^19.0.4",
"electron-builder": "^23.0.3",
"electron-packager": "^15.5.1",
"prettier": "1.17.0",
"wait-on": "^6.0.1"
}
}
I am getting a POST http://localhost:3000/handledata 500 (Internal Server Error)
when I am trying to post some data that I need to handle on the backend , below is the code for my front and backend anything missing ? or is there something wrong ? why would the post post to port 3000 instead of 5000 ? in my package.json I am calling a proxy is this even related to the error ?
// Fetch front end call
fetch('/handledata', {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({ a: 1, b: 2 }),
})
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
// Express server
import express from 'express';
import bodyParser from 'body-parser';
//import cors from 'cors'
import CryptoJs from 'crypto-js';
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());
const PORT = 5000; //process.env.PORT || 5000;
const router = express.Router();
// Test server
app.get('/test', (request: any, response: any) => {
response.send('working......................');
});
// Post data to handle
app.post('/handledata', (req: any, res: any) => {
console.log(req.body);
const handeleddata = // handledata logic could be anything !
res.json(handeleddata);
});
app.listen(PORT, console.log(`Server Started on Port ${PORT}`)!);
frontend package.json
{
"name": "app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#emotion/react": "^11.7.1",
"#emotion/styled": "^11.6.0",
"#material-ui/core": "^4.12.3",
"#material-ui/icons": "^4.11.2",
"#material-ui/lab": "^4.0.0-alpha.60",
"#paypal/react-paypal-js": "^7.5.1",
"#testing-library/jest-dom": "^5.16.2",
"#testing-library/react": "^12.1.3",
"#testing-library/user-event": "^13.5.0",
"#types/html2canvas": "^1.0.0",
"#types/jest": "^27.4.1",
"#types/jspdf": "^2.0.0",
"#types/node": "^16.11.26",
"#types/react": "^17.0.39",
"#types/react-dom": "^17.0.13",
"#types/react-router-dom": "^5.3.2",
"#types/uuid": "^8.3.3",
"axios": "^0.26.0",
"firebase": "^9.6.7",
"firestore-size": "^2.0.7",
"html2canvas": "^1.3.3",
"jspdf": "^2.4.0",
"notistack": "^1.0.10",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-ga": "^3.3.0",
"react-router-dom": "^5.3.0",
"react-scripts": "5.0.0",
"typescript": "^4.4.4",
"uuid": "^8.3.2",
"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"
]
},
"proxy": "http://localhost:5000"
}
Backend package.json
{
"name": "app",
"version": "1.0.0",
"engines": {
"node": "14.17.4",
"npm": "6.14.14"
},
"description": "",
"main": "index.js",
"scripts": {
"start": "node dist/server.js",
"client": "npm start --prefix client",
"clientinstall": "npm install --prefix client",
"dev": "concurrently \"nodemon src/index.ts\" \"npm run client\"",
"build": "tsc -p .",
"heroku-postbuild": "npm run build && NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"author": "me",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"client": "file:client",
"config": "^3.3.6",
"crypto-js": "^4.1.1",
"express": "^4.17.1",
"express-validator": "^6.12.1",
"firebase": "^9.6.7",
"jsonwebtoken": "^8.5.1",
"mongoose": "^6.0.6",
"npm": "^8.1.4",
"ts-node": "^10.5.0",
"typescript": "^4.4.4"
},
"devDependencies": {
"#types/crypto-js": "^4.1.1",
"#types/express": "^4.17.13",
"#types/node": "^17.0.18",
"concurrently": "^6.2.1",
"nodemon": "^2.0.12"
}
}
You need to modify your fetch request API url to give the complete hostname:
fetch('http://localhost:5000/handledata')
also you need to setup the cors in your backend
app.use(cors());
I am having a "Error: Invalid hook call. " issue after installing and using Framer-Motion. So I followed the debugging strategy from the React.js(https://reactjs.org/warnings/invalid-hook-call-warning.html) docs and the only thing that I see that might be causing this issue is last part before "Other Causes" where it talks about React Router Link.
here is the code to two files that caused this issue. Package.json file from outside the "client" directory (for the server):
"main": "server.js",
"scripts": {
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:prod": "node server.js",
"start:dev": "concurrently \"nodemon --ignore 'client/*'\" \"npm run client\"",
"client": "cd client && npm run start",
"install": "cd client && npm install",
"build": "cd client && npm run build",
"heroku-postbuild": "npm run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/MrDawit/Portafoglio_react.git"
},
"author": "",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/MrDawit/Portafoglio_react/issues"
},
"homepage": "",
"dependencies": {
"#popperjs/core": "^2.9.2",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"bootstrap": "^4.6.0",
"concurrently": "^5.3.0",
"cookie-parser": "^1.4.5",
"core-js": "^3.15.2",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"framer-motion": "^4.1.17",
"googleapis": "^67.0.0",
"if-env": "^1.0.4",
"nodemailer": "^6.4.17",
"nodemon": "^2.0.7",
"react": "^17.0.1",
"react-bootstrap": "^1.4.3",
"react-dom": "^17.0.1",
"run-all": "^1.0.1"
},
"devDependencies": {
"gh-pages": "^3.1.0"
}
And here is the code from the Package.json file inside the "client" directory for React:
"dependencies": {
"#testing-library/jest-dom": "^5.11.9",
"#testing-library/react": "^11.2.3",
"#testing-library/user-event": "^12.6.0",
"axios": "^0.21.1",
"gh-pages": "^3.1.0",
"http-proxy-middleware": "^1.0.6",
"nodemailer": "^6.4.17",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"web-vitals": "^0.2.4"
},
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "set Port=3080 && 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"
]
}
The issue ended up being that Framer Motion was supposed to be in the package.json file that is inside the "client" directory. Since Bootstrap and other similar modules were included in the outer package.json file and they installed correctly, it mislead me thinking that Framer Motion could do the same.
Steps for fixing my issue:
npm uninstall framer-motion
deleting both node_modules(inside and outside the "client" directory) directories
deleting both package-lock.json files
npm install
cd client
npm install framer-motion
Not doing step 5 in the first place was the cause of my error.
I have an app almost ready to package, I've been using express, react, bootstrap and electron. I can run the app file using npm start, But i cant package it. Build completes and when i run application file electron open but neither react runs on localhost nor express.
My directory structure is as follows
- api
- containes expressjs (package.json)
- client
- containes react and electron (package.json)
*let me know if you need more detailed structure
This is my procfile
react: npm run react-start
electron: npm run electron-start
express: npm start --prefix ../api/
This is my package.json
{
"name": "POSifier",
"version": "0.1.0",
"description": "system",
"private": true,
"homepage": "../",
"dependencies": {
"#date-io/date-fns": "^1.3.13",
"#material-ui/core": "^4.11.0",
"#material-ui/icons": "^4.9.1",
"#material-ui/pickers": "^3.2.10",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.5.0",
"#testing-library/user-event": "^7.2.1",
"#wojtekmaj/react-datetimerange-picker": "^3.0.0",
"bootstrap": "^4.5.2",
"canvasjs": "^1.8.3",
"custom-electron-titlebar": "^3.2.5",
"date-fns": "^2.16.1",
"jquery": "^3.5.1",
"jsbarcode": "^3.11.3",
"moment": "^2.29.1",
"mysql": "^2.18.1",
"pouchdb": "^7.2.2",
"pouchdb-authentication": "^1.1.3",
"pouchdb-browser": "^7.2.2",
"pouchdb-find": "^7.2.2",
"pouchdb-react-native": "^6.4.1",
"react": "^16.13.1",
"react-bootstrap": "^1.3.0",
"react-bootstrap-date-picker": "^5.1.0",
"react-dom": "^16.13.1",
"react-fade-in": "^1.1.1",
"react-native": "^0.63.2",
"react-pouchdb": "^2.0.1",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3",
"web-vitals": "^0.2.4"
},
"build": {
"appId": "com.Posifier.app",
"files": [
"build/**/*",
"node_modules/**/*"
],
"directories": {
"buildResources": "assets"
}
},
"scripts": {
"start": "nf start -p 3000",
"electron-start": "node src/wait-for-react",
"electron-pack": "build --em.main=build/electron.js",
"preelectron-pack": "yarn build",
"react-start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"electron": "electron .",
"pack": "electron-builder --dir",
"dist": "electron-builder",
"new":"react-scripts build && electron-builder --publish=always"
},
"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": {
"electron": "^11.1.1",
"electron-builder": "^22.9.1",
"foreman": "^3.0.1"
},
"main": "public/electron.js"
}