I have a Next.js application and I want to connect it to a Node.js backend.
If i put the file server.js in the same folder of the Next.js application I can run the application through these scripts:
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "set NODE_ENV=production node server.js",
"lint": "next lint"
}
I want to put all the backend files in another folder (server.js too) , but in this case I can't run the application.
What I have to do? Thanks
I have a React App with Node(Express) as the backend. React is requesting for an end point /api/users and Node is serving with a static response. This is working fine in local.
The same app has been deployed in firebase and the React part is not able to consume the Node /api/user part, api call failing.
Below is the project structure -
The node part is in server.js
Package.json has the proxy part directing to the Node Port
The scripts part in package.json is as given below -
"scripts": {
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"server": "nodemon server.js",
"start": "concurrently \"npm run server\" \"react-scripts start\""
},
Not sure what is missing? Just to reiterate that this is working in my local, but not working after deploying to firebase
I have a website that deployed to Herokuapp. But unfortunately, the herokuapp had a problem to upload an image to a local folder so I tried to deploy my website to another site that is Netlify.
Here comes the problem. I only knew the build setup just for deploying to herokuapp, so this is my setup :
My Folder structure :
/client <-- "this is my create-react-app folder"
/config
/models
/routes
/validation
package.json
server.js <-- "my server"
package.json
/* some settings */
"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
/* some settings */
I don't know how to run the "heroku-postbuild" setting on Netlify to make it work. How can I accomplish this?
Thank you very much for your help!
I am trying to target multiple environments from local while executing React app.
1. Development
2. Staging
3. Production
I am also trying to test for offline mode in any of the environments. So, the scripts what I have configured is as follows:
"staging-server": "nodemon server.js --environment=staging",
"staging": "concurrently -k \"npm:staging-server\" \"NODE_ENV='staging' PORT=3003 react-scripts start\"",
"prod": "npm run build && forever server.js --environment=production"
I am able to fetch environment arg using args inside my Express, but my local ui app is still showing development only when I console for process.env.NODE_ENV. I am also trying to set NODE_ENV with same line for staging, but still no luck. PORT setting is working but, the app is running in 3000 and 3003 both ports.
How to get rid of this? I would like to understand the staging configuration as well.
As per the docs, we cannot override NODE_ENV, but there is a room to create our own custom variables starting with REACT_APP_. So i configured to look as below:
Reference: https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables
"staging": "concurrently -k \"npm:staging-server\" \"cross-env REACT_APP_ENVIRONMENT='staging' PORT=3003 react-scripts start\"",
And inside my UI application, I can fetch its value by consoling it like this:
console.log('REACT_APP_ENVIRONMENT => ', process.env.REACT_APP_ENVIRONMENT);
I build the build with REACT_APP_STAGE and use it in my application as process.env.REACT_APP_STAGE.
"scripts": {
"analyze": "source-map-explorer 'build/static/js/*.js'",
"build-css": "node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/ --watch --recursive",
"start-js": "react-scripts start",
"start": "REACT_APP_STAGE=local npm-run-all -p watch-css start-js",
"build": "npm run build-css && react-scripts build",
"build-dev": "REACT_APP_STAGE=dev react-scripts build",
"build-prod": "REACT_APP_STAGE=prod react-scripts build",
"build-qa": "REACT_APP_STAGE=qa react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
Use cross-env in front of NODE_ENV.
npm i -g cross-env
"staging": "concurrently -k \"npm:staging-server\" \"cross-env NODE_ENV='staging' PORT=3003 react-scripts start\"",
Easiest approach is to add it directly in your command:
"scripts": {
"start": "./node_modules/.bin/nodemon server.js",
"start:prod": "NODE_ENV=prod node server.js",
},
These are my scripts:
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js",
"heroku-postbuild": "next build"
},
This is my procfile:
web: npm start -- --port $PORT
These are my config variables:
NPM_CONFIG_PRODUCTION=false
You have to update next to 5.0.1-canary.4 this was actually a nextjs bug.