How do I run a server and a React App at the same time? - npm

In a previous much more complicated project I built along side others in a class we were able to run npm start for the react app, and nodemon for the server at the same time without issue. I am now trying to build a simple single page web application with create-react-app and realize that I cannot run my server and my React App on the same port. I get a message that say "Something is already running on port 3000." Not sure how this was accomplished previously.
Do they even need to be running on the same port for them to communicate?
If yes or no, what is best practice for this?
Not sure what code to show so I have included my package.json and the code for my server.js so far. Any help or tips appreciated!
const express = require('express')
const app = express()
const cors = require("cors")
app.use(cors)
app.use(express.json())
app.listen(3000, () => {
console.log("Server is running")
})
{
"name": "project",
"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",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"pg": "^8.8.0",
"pg-hstore": "^2.3.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"sequelize": "^6.27.0",
"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"
]
},
"main": "./server/server.js"
}
So far I've tried to use concurrently, and npm-run-all but that seemed to break both the react app and the server.
Unfortunately the application that was previously built is broken repair (at least with my current skill set) excepting the npm start command. The major difference between the old project and my new one is that the old one has a different package.json for the server and the react app. Between the two packages I have all the same dependencies on my current project. Other than that the only difference I can find is that instead of the "main": "./server/server.js" in my single package.json, the react app has the following in it's react package.json:
"proxy": "http://127.0.0.1:4000"
Not sure if this comes into play at all because the react app from the old project still starts on port 3000. I have tried running my current project with the "proxy" code, but it doesn't have any affect.

When you run your server and client (React App) on the same machine or computer (meaning they are available through localhost), they should be running on different ports.
When "proxy": "http://127.0.0.1:4000" is found in the React App's package.json, it means that your client (React App) tries to call the server on port 4000.
You can read more about the "proxy" field in https://create-react-app.dev/docs/proxying-api-requests-in-development/.
By default, React App runs on port 3000. Therefore, I would suggest to change the port number in your server.js to 4000 for example.

If you want to make parallel command. i would recommend Lerna
which is very helpful for monorepo. ex: with single comannd we can download npm module for all project, Similarly can start all project with single cmd and same way build. flow this to get some idea lerna working sample
This loop has more info for the common node modules
As my opinion best option would be to use two port for serving client app and server, in dev environment.

Related

WebdriverIO : Can we push code with dependencies only installed?

I am using webdriverIO v6
I have Installed just these two packages: npm install #wdio/cli as well as webdriverio
my tests are ruining smoothly in my local.
Is this ok to push to code-repo in git, does this work in Jenkis or Azure devops?
or is is required to install the --save-dev too to work in CI tools?
{
"name": "test-package",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"wdio": "./node_modules/.bin/wdio wdio.conf.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#wdio/allure-reporter": "^6.1.23",
"#wdio/cli": "^6.1.25",
"#wdio/local-runner": "^6.1.25",
"#wdio/mocha-framework": "^6.1.19",
"#wdio/spec-reporter": "^6.1.23",
"#wdio/sync": "^6.1.14",
"chromedriver": "^83.0.1",
"wdio-chromedriver-service": "^6.0.3",
"webdriverio": "^6.1.25"
},
"dependencies": {}
}
This is nothing specific to wdio. This is a question which has been discussed multiple times in nodejs context.
Many developers suggest not to include node_modules in the repo because of various reasons which are logical. Then there are reasons which might force you to do it. if you are doing it just to reduce the build time, be prepared for other implications. Below are links which might help you.
https://flaviocopes.com/should-commit-node-modules-git/
Should "node_modules" folder be included in the git repository

How do you deploy api made with express in create react app

I have a simple api created with express.js. When ever i try to push to Heroku it get stuck at this point?
remote: [4/4] Building fresh packages...
remote: Done in 16.30s.
remote:
remote: -----> Build
remote: Running build (yarn)
remote: yarn run v1.22.4
remote: $ node server.js
remote: Server started on port 9000
I also see the same thing in heroku dashboard "View build progress"
here is my package.json
{
"name": "firebase-server",
"version": "1.0.0",
"main": "index.js",
"repository": "##########",
"author": "#######",
"license": "MIT",
"scripts": {
"start": "nodemon --ext js,graphql --ignore data/ server.js",
"build": "node server.js"
},
"dependencies": {
"#firebase/app": "^0.6.7",
"#firebase/component": "^0.1.15",
"apollo-server-express": "^2.15.1",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-jwt": "^6.0.0",
"firebase": "^7.15.5",
"graphql": "^15.3.0",
"jsonwebtoken": "^8.5.1",
"notarealdb": "^0.2.2"
},
"devDependencies": {
"nodemon": "^2.0.4"
}
}
the only thing i have in this project is my server.js. what am i missing or not doing. Basically i trying to deploy this to have a live API for my project. it is working locally but now i would like to have it working live on heroku.
With Heroku, the build script is used if you need to customize your build options (e.g. if you're using WebPack or TypeScript). The issue is that you're using a script called build, which you're expecting to start the application. It's running that script, as it promises to do, but since it's starting the application, it'll "hang forever" from your point of view.
If you change your scripts to this, I think this will solve your issues. Locally, you would use npm local for nodemon, and npm start would start the app on Heroku.
{
"scripts": {
"local": "nodemon --ext js,graphql --ignore data/ server.js",
"start": "node server.js"
},
}

nodemon not working

When i run nodemon, it shows
[nodemon] 1.17.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js`
But when i run npm start, everything works fine. Why is nodemon not working
my package.json
{
"name": "sarthakmedia",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "^1.18.3",
"cookie-parser": "~1.4.3",
"cors": "^2.8.4",
"debug": "~2.6.9",
"express": "~4.16.0",
"express-mysql-session": "^1.3.0",
"express-session": "^1.15.6",
"git": "^0.1.5",
"http-errors": "~1.6.2",
"jade": "~1.11.0",
"morgan": "~1.9.0",
"mysql": "*",
"nodemon": "^1.17.4",
"pug": "2.0.0-beta11"
},
"description": "practise",
"main": "app.js",
"devDependencies": {},
"author": "Anita",
"license": "ISC"
}
The api's don't get called at all. nodemon starts fine though
try this
first step
in package.json file
"scripts":{
"start":"node app",
"dev":"nodemon app"
}
second step
install nodemon as devdependencies
using code npm i -D nodemon
3)in app.js file
const express = require("express");
const app = express();
// routes
app.get("/",(req,res)=>{
res.send("hello");
});
// server started
const port =process.env.PORT || 5000;
app.listen(port,()=>{
console.log("server started at port 5000");
});
4)fourth step
use (npm run dev) in command line tool
Nodemon starts by running the file ./app.js.
But your start npm script (which works according to your question) runs a different file ./bin/www/index.js.
I think you should tell nodemon what file is your entry point.
Thus, you should edit the "main" entry in your package.json with the same value, e.g., ./bin/www. In fact, nodemon reads this value in your package.json in order to know what file to start with.
Just run your app with nodemon -w ./ when you are in app root directory.
-w stands for looking at the directory rather than 1 JS file. It is usefu; when you have more than 1 js you need to mon

Hot Reload not working in react-native android

I tried it on windows 10 and Ubuntu 16.04. In both cases the Hot Reload only works with changes in jsx, but not, when I change a variabel or function or something else in javascript. The Hot Reload is enabled. Live reload is disabled. When I save a change in the Visual Studio Code Editor, the virtual Device (android studio) reloads, but the changes are not there. The same on physical device in EXPO App.
First I didn`t install watchman. Hot Relaod not working. After installing it Hot Reload not working too.
I startet the app with create-react-native-app. It is not ejected.
package.json:
{
"name": "NativeReduxSaga",
"version": "0.1.0",
"private": true,
"devDependencies": {
"jest-expo": "25.0.0",
"react-native-debugger-open": "^0.3.17",
"react-native-scripts": "1.11.1",
"react-test-renderer": "16.2.0"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "node node_modules/jest/bin/jest.js",
"postinstall": "rndebugger-open --expo"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"#redux-offline/redux-offline": "^2.3.2",
"expo": "^25.0.0",
"prop-types": "^15.6.1",
"react": "16.2.0",
"react-native": "0.52.0",
"react-native-autocomplete-input": "^3.5.0",
"react-navigation": "^1.5.2",
"react-redux": "^5.0.7",
"redux": "^3.7.2",
"redux-devtools-extension": "^2.13.2",
"redux-logger": "^3.0.6",
"redux-observable": "^0.18.0",
"redux-promise-middleware": "^5.0.0",
"redux-saga": "^0.16.0",
"redux-thunk": "^2.2.0",
"rxjs": "^5.5.7"
}
}
watchman version
{
"version": "4.9.1",
"buildinfo": "94e66865386e844f2cffe52e355a94c96562d2e3 2018-03-12T19:58:02.0000000Z"
}
node version
v6.13.1
I read in some articles here, that it could help on Windows, to raise the MAX_WAIT_TIME. But I don´t have a \node_modules\react-native\node_modules\node-haste\lib\FileWatcher\ index.js file.
And why isn´t it working on Ubuntu , too?
Do I really need the watchman? How should it be configured? My .watchmanconfig file is an empty object like {}.
Is Hot Reload usually working with create-react-native-app?
Does anybody know what I can try here, to get the hot reload working?
Thanx for an help!
Actually there's a difference between Hot and Live Reloading.
Hot Reloading is instant reload while keeping the state of your application intact. However, it only works inside the render method and is only triggered on extensions of classes React.Component and Component
As in:
class A extends Component ...
class B extends React.Component ...
Live Reload on the other hand, rebuilds your application and discards your application state. It includes everything, from variables and methods to the simplest of string. That's why one will always start from the start screen of the application (because the state is lost).
Hope it helps!
Deleting the git index.lock file worked for me
rm -rf .git/index.lock
in android go to your MainApplication.Java
search for
#Override
public boolean getUseDeveloperSupport() {
return true;
}
Make sure it returns true. It might be the issue
I did the following steps to resolve this issue:
mkdir android/app/src/main/assets
react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
Shake android device or cmd + d in the simulator you will get a menu.
Once enable and disable Hot reload.
clear whatever the IP address you are using in Dev settings -> Debugging -> Debug server host & port for device.
Enable Hot reloading.
In terminal go to your project directory -> react-native start
react-native run-android or run it directly in your android studio.
Alternative solution:
"scripts": {
- "start": "react-native-scripts start",
+ "start": "DEBUG=true react-native-scripts start",
},
To have it work, you should make sure this file wasn't changed:
// ./android/app/src/main/java/com/cubyn/storage/MainApplication.java
...
#Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
...
IMHO it's a better solution because it will be enabled only during dev, and not when bundled for production.
For me the problem was with the AndroidX, basically some time you need to migrate to AndroidX due to the react-native old versions
just open your project in the Android Studio
Refactor -> Migrate to Android X ... and flow the instructions
and don't avoid to make backup when it will be promoted for save side
For anyone experiencing this problem when using WSL 2, the files need to exist inside of the Linux subsystem instead of inside any mounted Windows folders for the file watching to work properly. Taken from this post on Reddit:
They changed the file sharing protocol, from using they own custom developed protocol to using the 9P protocol, which at this time might not support file changes event.
I believe you can fix this issue by putting your code on the Linux file system (ex: in your user's home directory), and access these files through the WSL share, \wsl$\DISTRO_NAME from Windows.
In the Windows explorer, if you go to \wsl$, you should see all your WSL Linux distros installed and can access all the files on their file systems.
I think this is strait forward
Also dont forget to run npm start in your text editor and not outside editor like git bash
Make sure you don't have an index.lock file in your .git directory :) Deleting it could solve your issue.

React-native install issue in expo app

I'm trying to learn react-native by creating an app using the expo development environment. I had a working app (little more than the code that shipped with expo) until installing redux. Currently I am getting the following error from the XDE:
Problem checking node_modules dependencies: Unexpected end of JSON input
and the following from the ios simulator:
undefined is not an object (evaluating 'ReactPropTypes.string')
Package.json:
{
"name": "myApp",
"version": "0.0.0",
"description": "Hello Expo!",
"author": null,
"main": "main.js",
"scripts": {
"test": "node node_modules/jest/bin/jest.js"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"#expo/ex-navigation": "~3.0.0",
"#expo/samples": "~1.0.3",
"babel-preset-react": "^6.24.1",
"expo": "17.0.0",
"react": "^16.0.0-alpha.12",
"react-native": "^0.45.1",
"react-redux": "^5.0.5",
"redux": "^3.6.0"
},
"devDependencies": {
"jest-expo": "~1.0.1"
}
}
I believe my node modules contain valid JSON. It should be noted that I'm using a more current version of react-native than expo. Is this an issue with the packages I have installed? Which files would be helpful in solving this?
Although I haven't been able to fix this particular error. This one and many others can be avoided by using yarn instead of npm when working with expo (I have no affiliation with either tool).
I believe this is due to a bug in the current release of npm 5. As mentioned in the other answer here, using npm 4 or yarn will resolve this problem.