Heroku serving create-react-app development build instead of production - reactjs

I'm new to create-react-app.
I just made a fresh setup with redux and react-router-dom then pushed it to Scalingo then Heroku and both of them ends up serving the development build. My redux-logger is on and React dev tools warns that :
This page is using the development build of React. 🚧
I did not do anything to customize deployment, just pushed to production.
What am I doing wrong ?
Scalingo deployment logs :
<-- Start deployment of ***** -->
Fetching source code
Fetching deployment cache
-----> Creating runtime environment
NPM_CONFIG_LOGLEVEL=error
NPM_CONFIG_PRODUCTION=true
NODE_VERBOSE=false
NODE_ENV=production
NODE_MODULES_CACHE=true
-----> Installing binaries
engines.node (package.json): unspecified
engines.npm (package.json): unspecified (use default)
engines.yarn (package.json): unspecified (use default)
Resolving node version 8.x...
Downloading and installing node 8.15.0...
Using default npm version: 6.4.1
Resolving yarn version 1.x...
Downloading and installing yarn (1.14.0)...
Installed yarn 1.14.0
-----> Restoring cache
Loading 2 from cacheDirectories (default):
- node_modules
- bower_components (not cached - skipping)
-----> Building dependencies
Installing node modules (yarn.lock)
yarn install v1.14.0
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.60s.
Running build (yarn)
yarn run v1.14.0
$ react-scripts build
Creating an optimized production build...
Compiled successfully.
File sizes after gzip:
161.32 KB build/static/js/2.21f749f2.chunk.js
1.15 KB build/static/js/main.e65e7a00.chunk.js
761 B build/static/js/runtime~main.fdfcfda2.js
The project was built assuming it is hosted at the server root.
You can control this with the homepage field in your package.json.
For example, add this to build it for GitHub Pages:
"homepage" : "http://myname.github.io/myapp",
The build folder is ready to be deployed.
You may serve it with a static server:
yarn global add serve
serve -s build
Find out more about deployment here:
https://facebook.github.io/create-react-app/docs/deployment
Done in 7.79s.
-----> Caching build
Clearing previous node cache
Saving 2 cacheDirectories (default):
- node_modules
- bower_components (nothing to cache)
-----> Build succeeded!
Build complete, shipping your container...
Waiting for your application to boot...
<-- https://*****.scalingo.io -->
package.json:
{
"name": *****,
"version": "0.1.0",
"private": true,
"dependencies": {
"#sentry/browser": "^4.5.4",
"husky": "^1.3.1",
"lint-staged": "^8.1.3",
"prettier": "^1.16.4",
"prop-types": "^15.7.1",
"react": "^16.8.1",
"react-dom": "^16.8.1",
"react-redux": "^6.0.0",
"react-redux-i18n": "^1.9.3",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.5",
"redux": "^4.0.1",
"redux-logger": "^3.0.6",
"redux-promise": "^0.6.0",
"redux-thunk": "^2.3.0"
},
"lint-staged": {
"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
"prettier --single-quote --trailing-comma all --write",
"git add"
]
},
"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"
],
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
}

The server in Heroku will run the start script given in the file package.json. By default, when using create-react-app the start script will run the app in development mode.
In order to run the optimized application from the build folder you will need to write a server. You can use the following code for a simple server. Make sure to save it a file named server.js and place it in the root of the repository.
const express = require('express');
const favicon = require('express-favicon');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
app.use(favicon(__dirname + '/build/favicon.ico'));
// the __dirname is the current directory from where the script is running
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, 'build')));
app.get('/ping', function (req, res) {
return res.send('pong');
});
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(port);
You will need to add the following dependencies:
express
express-favicon
path
Finally, change the package.json so the start script runs the server. To keep being able to run the development mode, you can provide another script for it.
"scripts": {
"build": "react-scripts build",
"devstart": "react-scripts start",
"start": "node server.js"
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Push the changes to Heroku and your application should run properly.
You can read more about it in this article.
https://medium.com/jeremy-gottfrieds-tech-blog/tutorial-how-to-deploy-a-production-react-app-to-heroku-c4831dfcfa08

My working solution is just modifying the package.json to:
"scripts": {
"heroku-prebuild": "npm install -g serve",
"devstart": "react-scripts start",
"start": "serve -s build",
"build": "react-scripts build",
"eject": "react-scripts eject",
},
With "heroku-prebuild" you can install the serve wihtout having to upload any additional code.

I added a Procfile with the correct execution command and included a prebuild step for heroku in package.json scripts:
package.json:
"scripts": {
"heroku-prebuild": "npm install -g serve", // <- NEW
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
},
Procfile:
web: serve -s build

Related

Stuck at Edit src/App.js and save to reload at heroku

I am extremely new to React, I successfully push my code in Heroku at https://react-heroku-juvie.herokuapp.com/ ,but I'm still stuck at this
Edit src/App.js and save to reload.
What I did is
npx create-react-app react-heroku
cd react-heroku
npm run build
npm i -g heroku (...login creds)
heroku create react-heroku-juvie
git remote add heroku
git add.
git commit -m "Initial Commit"
git push heroku master
I want my app to function and display. How do I do this? Here's my src code https://github.com/juvielone/pocketNote.git
I had the same problem but in fact after running your app locally you are supposed to produce a build, see https://create-react-app.dev/docs/deployment/
I don't know how to proceed with Heroku, you could begin with the command:
$ npm run build
I guess it's quite complicated with heroku, so I suggest you try with Github Pages.
Here is what I did with github pages: I began with the command:
$ npm install --save gh-pages
(in the main directory of my app).
After that in my package.json file I added 3 lines: 1 for my homepage (line 5) and 2 for the "predeploy" and "deploy" scripts in the scripts section (lines 15 and 16), as here (it's the beginning of my package.json file):
{
"name": "client",
"version": "0.1.0",
"private": true,
"homepage": "https://SebastienBosca.github.io/defi3w",
"dependencies": {
"bootstrap": "^5.1.3",
"react": "^17.0.2",
"react-bootstrap": "^2.2.3",
"react-dom": "^17.0.2",
"react-scripts": "3.2.0",
"web3": "^1.6.1"
},
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
After that I ran
$ npm run deploy
(which runs npm run build and gh-pages -d build); I am asked my Username (put your email or usernam on github) and my password (here, putting your Personnal Access Token is mandatory, you password won't work).
That's all !

electron with react error bin sh build command not found

I have an electron/react application which works fine in development mode, but when it comes to building and packaging it gives this error when i run yarn electron-pack
$ build -mw
/bin/sh: build: command not found
error Command failed with exit code 127.
this happened after i installed some additional packages ( the packaging before that worked fine )
this is my scripts in package.json file :
"scripts": {
"start": "rescripts start",
"build": "rescripts build",
"test": "rescripts test",
"eject": "react-scripts eject",
"electron-dev": "concurrently \"BROWSER=none yarn start\" \"wait-on
http://localhost:3000 && electron .\"",
"postinstall": "electron-builder",
"preelectron-pack": "yarn build",
"electron-pack": "build -mw"
},
and this is the devDependencies
"devDependencies": {
"#rescripts/cli": "^0.0.16",
"#rescripts/rescript-env": "^0.0.14",
"concurrently": "^6.2.0",
"electron": "^13.1.8",
"electron-builder": "^22.11.7",
"typescript": "^4.3.5",
"wait-on": "^6.0.0"
}
a temporary solution that i found is removing node modules files and run npm install
rm -rf node_modules
npm install

React Material start script NODE_PATH not recognized

I have downloaded following React Material template
template
Followed steps from documentation
package.json
{
"name": "material-dashboard-react",
"version": "1.6.0",
"description": "Material Dashboard React. Coded by Creative Tim",
"private": false,
"main": "dist/index.js",
"scripts": {
"start": "NODE_PATH=./src react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"install:clean": "rm -rf node_modules/ && rm -rf package-lock.json && npm install && npm start",
"lint:check": "eslint . --ext=js,jsx; exit 0",
"lint:fix": "eslint . --ext=js,jsx --fix; exit 0",
"build-package-css": "cp src/assets/css/material-dashboard-react.css dist/material-dashboard-react.css",
"build-package": "npm run build-package-css && babel src --out-dir dist"
},
}
Getting the following error
It is because you are using Windows. The project was likely ran on unix based computers before (NODE_PATH=./src is not a windows way of defining environment variables). You can either fix it by using the Windows syntax "start": "set NODE_PATH=./src react-scripts start", (your project will not run on unix machines) or use a cross-env library for defining your environment : https://www.npmjs.com/package/dotenv
For anyone wondering, I had the same issue with the same template and my solution was to configure a file in the project root as specified in this link.
I'm using docker and my working directory is in the /app directory, so I added this line in my tsconfig.json file at the root of my react app folder:
"compilerOptions": {
...,
"baseUrl": "/app/src",
}

Building a dev version of a react application, continuously, for development

I just created a react application by running:
create-react-app foo
which created a package.json file with:
{
"name": "foo",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-scripts": "2.1.3"
},
"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"
]
}
npm start and npm run build both run as intended, but this application is the frontend to a Django application, so, to have dev mimic production a bit better I don't want to serve the app with node.js. What I want to do is continuously build the assets, html, js, etc and let Django serve them like any other HTML/JS/CSS. I remember Webpack doing this last time I played with it a couple of years ago.
How do I do it in the context of this react application? In short, how do I make npm run build run continuously on file changes and generate dev assets.
You can install the watch package as a dev-dependency and add a script in your package.json that watch for changes in src folder and run npm run build:
npm install --save-dev watch
// package.json
"scripts": {
"watch": "watch 'npm run build' src/"
}
Then, while you work, keep open a tab with:
npm run watch
Consider, though, that the build could be a long process. It depends on the size of your application.

Chrome Extension + create-react-app live reload

Any advise on how to achieve live-reloading when implementing a Chrome Extension with create-react-app? Currently I yarn run build every time there is a change.
I managed to do that using create-react-app by:
npm i npm-watch --save-dev
Then in the package.json
{
"name": "react-app",
"version": "0.1.0",
"private": false,
"devDependencies": {
"npm-watch": "^0.1.8",
"react-scripts": "0.9.5",
},
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"watch": "npm-watch" //add this to the script
},
"watch": { //add this outside the script
"build": "src/"
}
}
I got this working with fswatch on Mac (had to brew install fswatch):
fswatch -o ~/$PATH_TO_YOUR_PROJECT/src | xargs -n1 -I{} npm run build
This will run npm run build anytime the src directory changes (which is what I was doing manually beforehand anyways)
Note: my manifest is pointing to the build directory for my popup.
I achieve live-reloading when implementing a Chrome Extension V3 with create-react-app. Whatever you change page or content script, all things is auto refresh/reload.
https://github.com/Godiswill/cra-crx-boilerplate

Resources