Deploying React app with Parcel build on Github Pages - reactjs

I am trying to deploy a React app with Parcel on Github Pages.
I have deployed it but the app is not actually rendering on the screen. I don't understand what I am doing wrong.
Package.json
{
"name": "pet-adoption-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"homepage": "https://github.com/junaidshaikh-js/pet-adoption-app",
"scripts": {
"format": "prettier --write \"src/**/*.{js,jsx}\"",
"lint": "eslint \"src/**/*.{js,jsx}\" --quiet",
"dev": "parcel src/index.html --public-url /pet-adoption-app/",
"predeploy": "npm build",
"deploy": "gh-pages -d dist"
},
"repository": {
"type": "git",
"url": "git+https://github.com/junaidshaikh-js/pet-adoption-app.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/junaidshaikh-js/pet-adoption-app/issues"
},
"devDependencies": {
"#babel/core": "^7.12.16",
"#babel/eslint-parser": "^7.13.4",
"#babel/plugin-proposal-class-properties": "^7.13.0",
"#babel/plugin-transform-runtime": "^7.16.4",
"#babel/preset-env": "^7.13.5",
"#babel/preset-react": "^7.12.13",
"eslint": "^8.3.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.2.0",
"gh-pages": "^3.2.3",
"parcel": "^1.12.3",
"prettier": "^2.4.1"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0"
}
}
Here is the live deployed link: https://junaidshaikh-js.github.io/pet-adoption-app/
Here is the Github Repo: https://github.com/junaidshaikh-js/pet-adoption-app

After hours of research, I found out that when you ae using react-router-dom > v4 and deploying to a sub-domain (for example //pet-adoption-appk in your case) you have to provide a basename attribute on the <BrowserRouter> with the sub-domain value. in your case you have to pass it like this <BrowserRouter basename="/pet-adoption-app">

As #tromgy pointed out in the discussion, the root of the problem is that the generated html pages contain / characters before the url references to the various assets.
For example, if you have an html file at domain.com/subfolder/index.html that contains <script src="/script.js">, the browser will look for the script at domain.com/script.js instead of domain.com/subfolder/script.js.
If you instead remove the / (e.g. <script src="script.js">) the browser will look for the script relative to the location where the html file was (e.g. domain.com/subfolder/script.js), which is probably what you want.
Parcel supports a --public-url option to accomplish this (see cli docs and target docs). By passing a . as a parameter, you can tell parcel to omit the / from the output. So your build script would be something like:
parcel build src/index.html --public-url .
(strangely, in your question, you showed a dev command that used this option, but I didn't see that code in the example repo)
Another thing to note is that you're using the unsupported v1 version of parcel. I upgraded you to v2 and fixed the issue above in this PR.

Step 1
git remote add origin [YOUR REPO LINK]
git add -A
git commit -m "Initial commit"
git push -u origin main
Step 2
"homepage": "https://[USERNAME].github.io/[YOUR REPO NAME]",
Step 3
npm install gh-pages --save-dev
Step 4
Add a script in your package.json file
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
Run the command given Below
npm run deploy

Related

Npm script for building react app working locally but not in pipeline

I have the following package.json for my React project
{
"name": "webforms-react-sample",
"version": "1.0.0",
"description": "Sample projects for hosting React apps in a WebForms application.",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --mode development --config webpack.dev.js",
"build-dev": "npx webpack --config webpack.dev.js",
"build-prod": "npx webpack --config webpack.prod.js"
},
"author": "Dimitris Vardalis",
"license": "ISC",
"devDependencies": {
"#babel/cli": "^7.1.0",
"#babel/core": "^7.1.0",
"#babel/preset-env": "^7.1.0",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.2",
"css-loader": "^6.7.3",
"react": "17.0.2",
"react-dom": "17.0.2",
"style-loader": "^0.23.0",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.11.1",
"webpack-merge": "^5.8.0"
},
"dependencies": {
"react-hot-loader": "^4.6.5",
"react-router-dom": "^5.2.0"
}
}
I'm trying to run the build-dev script. When trying locally I can make that script to work as is expected so build is made. But when going to the build pipeline is returning me this:
npm WARN exec The following package was not found and will be installed: webpack#5.75.0
CLI for webpack must be installed.
> webforms-react-sample#1.0.0 build-dev
webpack-cli (https://github.com/webpack/webpack-cli)
We will use "npm" to install the CLI via "npm install -D webpack-cli".
> npx webpack --config webpack.dev.js
Do you want to install 'webpack-cli' (yes/no):
##[warning]Couldn't find a debug log in the cache or working directory
##[error]Error: Npm failed with return code: 1
I'm using Azure Devops and a windows machine agent (always using the latest version) for running the pipeline. And yes I'm installing npm packages before the step when trying to run that script. I don't have any npm cache step to avoid Azure caching old files and not updating on releases (this happened to me on other pipelines).
Current pipeline is this (see pictures)
pipeline steps part 1
Pipeline steps part2
I have already tried uninstalling the packages and reinstalling them, same results and been breaking my head for a while and still the same results.
Well I made it work by changing npm install step on React to npm install --force.

No Build folder

I'm using a parcel bundler instead of a react-scripts which uses npm run build-prod instead of npm run build but it doesn't create a build folder instead they all in a dist folder so i changed the deploy script in the package.json to "deploy": "gh-pages -d dist" instead of the standard "deploy": "gh-pages -d build" which will give an error saying build script is missing , after i tried deploying this to git hub pages but i get an empty screen
"scripts": {
"predeploy": "npm run build-prod",
"deploy": "gh-pages -d dist",
"clean": "rm dist/bundle.js",
"start": "parcel src/index.html",
"build-prod": "parcel build src/index.html"
},
"dependencies": {
"node-sass": "^7.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.3.0"
},
"devDependencies": {
"#babel/core": "^7.18.5",
"#babel/preset-env": "^7.18.2",
"#babel/preset-react": "^7.17.12",
"gh-pages": "^4.0.0",
"parcel-bundler": "^1.12.5",
"prettier": "^2.7.1"
}
According the the Parcel docs here, you need to specify the --dist-dir flag.
Change your "build-prod" script to this:
"build-prod": "parcel build src/index.html --dist-dir build"
Now the output directory for your parcel build will go to build instead of dist so you can change your "deploy" script back to this:
"deploy": "gh-pages -d build"

react-scripts build failing when updating from 1.x to 3.0.1

I'm trying to migrate react-scripts from my current version (1.1.4) to the latest 3.0.1 for obvious reasons, but I'm having a hell of a time.
Here's my current, build successful package.json
{
"name": "name",
"version": "0.1.0",
"homepage": ".",
"private": true,
"dependencies": {
"ajv": "^6.9.1",
"axios": "^0.18.0",
"axios-debug": "0.0.4",
"date-fns": "^1.30.1",
"formik": "1.2.0",
"history": "4.7.2",
"moment": "2.22.2",
"localforage": "1.7.3",
"prettier": "1.14.2",
"react": "^16.8.1",
"react-bootstrap": "0.32.4",
"react-checkbox-group": "4.0.1",
"react-datetime": "2.15.0",
"react-dom": "^16.3.2",
"react-render-debugger": "1.0.2",
"react-router-dom": "4.3.1",
"react-scripts": "1.1.4",
"semantic-ui-css": "^2.4.1",
"semantic-ui-react": "^0.87.1",
"styled-components": "^4.2.0",
"react-router-hash-link": "1.2.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
So I'm using npm so running npm install --save --save-exact react-scripts#3.0.1 installs the new react-scripts version, everything is updated correctly. I updated a few other packages that were needed as deps. No security vulnerabilities, everything was clean and installed correctly.
I'm using maven as a build system to build my app, once it tries to execute npm run build it fails on react-scripts build
The debug log is super unhelpful. The only error displayed is /node_modules/.bin/react-scripts: Permission denied
I've tried changing the permissions using chmod +x /node_modules/.bin/react-scripts but no luck.
I've tried deleting node_modules several times and reinstalling the modules using npm ci. Just about any issue I've found on github I've tried.
I'm not sure what else could be wrong.
Using node v11.9.0 and npm 6.5.0
Do I need to update in smaller version increments? I'm really at a loss
EDIT:
Something is wrong with npm in my case. Manually updating the package versions in package.json seems to work for now... I need to figure out why npm is broken for me

NPM installing nearly 202M of modules - this can't be right

So I am looking at my package.json file - and nothing looks particularly crazy:
{
"name": "application_name",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap-toggle": "^2.2.2",
"classnames": "^2.2.6",
"html-react-parser": "^0.4.6",
"jquery": "^3.3.1",
"react": "^16.4.1",
"react-bootstrap-toggle": "^2.3.1",
"react-cookie": "^3.0.4",
"react-dom": "^16.5.2",
"react-load-script": "0.0.6",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.4",
"react-timer-mixin": "^0.13.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build && robocopy .\\build ..\\www /MIR",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
I've tried npm install, npm install --production and npm install --express
No matter what npm command I write, I get the following:
added 1425 packages from 862 contributors and audited 14585 packages in 15.876s
It ultimately is 202M - this can't be right, that's a HUGE number of modules
This is for a React project I am working on.
I only made this question because none of the other answers seemed to work for my situation.
Is there a solution to this problem? I am intending on compiling this into a mobile app and 202M is a huge footprint for what is a pretty simple app.
202Mb is almost completely occupied with development dependencies, react-scripts.
react-scripts is a bundle consisting Webpack and other tools that are necessary to run the project. It is provided by Create React App, which is just an executable that creates boilerplate project with react-scripts:
This package includes scripts and configuration used by Create React
App.
node_modules size is adequate for complex configuration that CRA sets up. It doesn't affect the size of compiled application.

Error using Chrome React Developer Tools with create-react-app

After creating a new create-react-app project I've run into a bit of a big problem when I run npm start inside the project. Chrome React Developer tools does not show all files in src folder. Instead it shows only folder ./src
Chrome React Developer Tools
I am using nodejs 6.10.1 , create-react-app 1.3.0.
package.json:
{
"name": "test",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.16.1",
"bootstrap": "^3.3.7",
"prop-types": "^15.5.9",
"react": "^15.5.4",
"react-bootstrap": "^0.30.10",
"react-bootstrap-table": "^3.2.1",
"react-dom": "^15.5.4",
"react-redux": "^5.0.4",
"react-router": "^3.0.0",
"recharts": "^0.22.3",
"redux": "^3.6.0",
"redux-logger": "^3.0.1",
"redux-promise-middleware": "^4.2.0",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"react-scripts": "0.9.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
Any advice how to fix it? I checked documentation but didn't find any solution.
You can still debug the code using VScode Chrome extension, however its not my favorite way of debugging a frontend application.
Use eject command to enable debugging.
After you npm run eject, it reveals more configurations for you to edit, one of those is source-map type (under a webpack configuration). Once you eject, the source-map type changes thus allowing you to debug on chrome-dev-tools.

Resources