Trying to deploy a create-react-app project to Netlify - javascript

My current project is having trouble building in the Netlify platform. I am able to build my app locally just fine and host it through gh-pages, but when I try to build the same app, I run into this error.
Netlify Deploy Log
11:10:03 PM: Failed to compile.
11:10:03 PM:
11:10:03 PM: ./src/pages/Mortgage.js
11:10:03 PM: Cannot find file '../hooks/UseWindowSize.js' in './src/pages'.
Current Folder Structure
src
--hooks
----UseWindowSize.js
--pages
----Home.js
----Mortgage.js
Any idea what the issue might be?

This is how I managed to get it working.
I added the site on Netlify by linking it via a Github repository. Then inside on Netlify, I did the flowing:
Click on the Sites menu link and select the site you would like to deploy
To access the Settings, click on either:
The Site Settings button - is located in the first block/widget below the menu
Deploys in the menu next to Site Overview then click the Deploy setting button - located in the first block/widget below the menu
Below the first block/widget click the link Build & deploy -
located in the left side navigation aside
In the Build settings widget block, click the Edit settings button.
Change Base directory to ./
Change Build command to npm run build
Change Publish directory to ./build
Scroll down to Environment variables and add the key PRODUCTION, set the value of PRODUCTION to true
Retry your build to see if the configuration worked.
Side note: It's a fresh create react app react install, no 3rd party librarys yet.
Happy Coding =)
Pro Tip - create a netlify.toml file in the root of your project and add the following:
# Production context:
# All deploys from the main repository branch
# will inherit these settings.
[context.production]
command = "npm run build"
[context.production.environment]
PRODUCTION = "true"
Ive noticed that using a boolean value as a string works but if you use it like normal the build breaks

To be honest, using Vercel with React is way easier, and it's also better.
Push project onto a repository, go to vercel's site, login with Github, and choose your repository and it does 95% for you.
Netlify also has some problems with React-router, so Vercel is over all a clear winner.

Related

Node cannot find path in node_modules / 'react-scripts' is not recognized as an internal or external command

I'm on windows 10
When I run:
npm run start
or
npm start
I get an error:
npm start
> app-test#0.1.0 start
> react-scripts start
'react-scripts' is not recognized as an internal or external command,
operable program or batch file.
What I already tried:
npm ci
remove node_modules and reinstall project
npm audit fix
run command from elevated PowerShell (as admin)
Uninstall Node v.18 win NVM and reinstall common Node installation LTS v.16
Check if react scripts is on dependencies section in package.json - and it's of course there.
Recreate project itself and also create a fresh new React project with npx create-react-app
Clearing npm cache
Nothing's helped me.
BUT This project runs fine WHEN I explicitly tell the node path to subfolder with a script:
app-test> node node_modules/react-scripts/scripts/start.js
Compiled successfully!
You can now view app-test in the browser.
Local: http://localhost:3000
On Your Network: http://192.168.91.1:3000
Note that the development build is not optimized.
To create a production build, use npm run build.
webpack compiled successfully
What's interesting that an old React projects also resided in the parent folder starts normally even I renew node_modules in these projects doing them npm i or npm ci
But any time I try to create and start new React app command npm start fails
UPD1: Tried to create-react-app on any other drive or folder. And it's running ok.
So culprit seems permissions policies in my common working directory ?
UPD2:
I resolved a problem though still have no clear idea who was actually the really culprit of this bug.
I copied all my parent developer folder with all code examples to another drive, then tried to create react app there and out of the blue it worked fine at then new location.
Also I removed this original folder from the drive where it used to be. And do git clone "..." at this directory< recreating the exact structure as it was before all experiments.
Then I tried create-react-app exactly at that location as I've unsuccessfully tried before swapping folders and it was OK !
Tried couple of time with different folder location within parent directory and all sill working fine now !
I remembered now I already have such bug before. And I had to reinstall Windows that time.
I also want to mention that I also have another machine with Windows, another one with Linux and had also laptop with an OSX so I can ( or could) push to this very git repository from any of these computers.
Suggestion of somebody who have any idea, why this bug was happening repeatable would be highly appreciated.
Thanks.

How to create files using JavaScript in GitHub? [duplicate]

I made a website using Node.js as the server. As I know, the node.js file should start working by typing commands in terminal, so I'm not sure if Github Pages supports node.js-hosting. So what should I do?
GitHub pages host only static HTML pages. No server side technology is supported, so Node.js applications won't run on GitHub pages. There are lots of hosting providers, as listed on the Node.js wiki.
App fog seems to be the most economical as it provides free hosting for projects with 2GB of RAM (which is pretty good if you ask me).
As stated here, AppFog removed their free plan for new users.
If you want to host static pages on GitHub, then read this guide. If you plan on using Jekyll, then this guide will be very helpful.
We, the Javascript lovers, don't have to use Ruby (Jekyll or Octopress) to generate static pages in Github pages, we can use Node.js and Harp, for example:
These are the steps. Abstract:
Create a New Repository
Clone the Repository
git clone https://github.com/your-github-user-name/your-github-user-name.github.io.git
Initialize a Harp app (locally):
harp init _harp
make sure to name the folder with an underscore at the beginning; when you deploy to GitHub Pages, you don’t want your source files to be served.
Compile your Harp app
harp compile _harp ./
Deploy to Gihub
git add -A
git commit -a -m "First Harp + Pages commit"
git push origin master
And this is a cool tutorial with details about nice stuff like layouts, partials, Jade and Less.
I was able to set up github actions to automatically commit the results of a node build command (yarn build in my case but it should work with npm too) to the gh-pages branch whenever a new commit is pushed to master.
While not completely ideal as i'd like to avoid committing the built files, it seems like this is currently the only way to publish to github pages and should work for any frontend Node.js app (or app built with a frontend framework like React or Vue) that can be served as static files.
I based my workflow off of this guide for a different react library, and had to make the following changes to get it to work for me:
updated the "setup node" step to use the version found here since the one from the sample i was basing it off of was throwing errors because it could not find the correct action.
remove the line containing yarn export because that command does not exist and it doesn't seem to add anything helpful (you may also want to change the build line above it to suit your needs)
I also added an env directive to the yarn build step so that I can include the SHA hash of the commit that generated the build inside my app, but this is optional
Here is my full github action:
name: github pages
on:
push:
branches:
- master
jobs:
deploy:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout#v2
- name: Setup Node
uses: actions/setup-node#v2-beta
with:
node-version: '12'
- name: Get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Cache dependencies
uses: actions/cache#v2
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- run: yarn install --frozen-lockfile
- run: yarn build
env:
REACT_APP_GIT_SHA: ${{ github.SHA }}
- name: Deploy
uses: peaceiris/actions-gh-pages#v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./build
Alternative solution
The docs for next.js also provides instructions for setting up with Vercel which appears to be a hosting service for node.js apps similar to github pages. I have not tried this though and so cannot speak to how well it works.
No, You cannot publish on Github pages. Try Heroku or something like that. You can only deploy static sites on github pages. You can't deploy a server on github pages.
No,
GitHub allows hosting only static websites(having only HTML, CSS, javascript).
Dynamic websites(having databases, servers, and all) can't be hosted as a Github page.
And node.js app is a server-based website, we can't host it on Github.
You can try Heroku, Openshift to host your website.
ahm. Yep, as most answer says. Github Pages only process html and css and a front-end JS.
But you can use JS framework like Gatsby which is mainly known to generate static purely static files, it gathers the data on compilation.
Then use that generated folder as the directory of the site.
I would like to add that it IS very much possible, as I am doing it right now. Here's how I'm doing it:
(I'm going to assume you have a package and/or directory ready to publish.)
In the root of your package.json, add
"homepage": "https://{pages-endpoint}/{repo}",
Where the pages-endpoint is the blah.github.io endpoint you specified in the Settings -> Pages portion of your repository, and repo is the name of your repository.
Then make sure you npm install --global gh-pages --save-dev. You need the --global to ensure the bin file is on your PATH and --save-dev should add it as a dependency in your package.json
After that, just npm run build && gh-pages -d build. The -d specifies your output build directory. The standard is build, but mine was public. If it's different, just change it.
Lastly, make sure in the Settings -> Pages section, you select gh-pages as the branch to host and leave the directory as / (root). Once it's built, your site should be available at your github.io endpoint.
Happy Dev-ing!
It's very simple steps to push your node js application from local to GitHub.
Steps:
First create a new repository on GitHub
Open Git CMD installed to your system (Install GitHub Desktop)
Clone the repository to your system with the command: git clone repo-url
Now copy all your application files to this cloned library if it's not there
Get everything ready to commit: git add -A
Commit the tracked changes and prepares them to be pushed to a remote repository: git commit -a -m "First Commit"
Push the changes in your local repository to GitHub: git push origin master

react serve works, but deploying to github pages or heroku fails

I'm trying to deploy this repo
https://github.com/stepseazy/checkers/
to this website
https://stepseazy.github.io/checkers
However, I'm getting 404 errors. I also tried heroku. It does work when I serve the build locally. Not sure what's wrong. Please help!!!
You'll have to distinguish your dev environment and your production one. On dev mode, you run npm start that does things (as instructed in package.json) and act as a local server and serves you project on http://localhost:3000. You simply cannot use that in production mode on Github or Heroku.
You need to deploy the built version, running npm run build. It will create a javascript file in a dist folder. You might want, on Github, create a gh-pages branch, build this production ready file on it, create an index.html file that serves it, and commit that.
I'm no expert on Heroku but it might be a slightly similar case (run post-deploy scripts that build your app and serve it through an index.html file).
Hope that help.

Setting up Gatsby on GitHub Pages

I've been having a nightmare setting up a reactjs web app/webpack etc myself. So now I'm using Gatsby, which has been a life saver!!!!
I've installed Gatsby locally using NPM. And I want to deploy it to my Github page... It's kinda worked...
Here are the instructions I've followed:
https://www.gatsbyjs.org/docs/deploy-gatsby/#github-pages
And here is what I'm seeing on my GitHub.io page:
https://reenaverma.github.io/
Its not the default starter pack index page/content, I'm seeing on my localhost 8000.
I can see on my Github repo the correct content has deployed to Github:
https://github.com/ReenaVerma/reenaverma.github.io
But I'm not actually seeing it on my URL. It looks like it's surfacing my readme first... How can I fix this?
Any ideas?
I see that your gatsby project is your main gh-pages. Therefore, you need to put your build in the master branch not in the gh-pages branch.
So you need to put your src code in a branch of your choice & in the master branch you put your generated build.
Change your script to the following
"scripts": {
"deploy": "gatsby build && gh-pages -b master -d public",
}

Node.js create-react-app build

I am pretty new to web development and I was asked to create a single-page application with tools of my choice. The only requirement is that it has to run locally on a GlassFish server (in NetBeans: Java Web > Web Application). I use the create-react-app starter kit provided by Facebook to build the application. When I run npm run build I get a build folder containing an html-file and a js-file. But when I double-click the html-file, the browser opens and just shows an empty page. Does anyone know what I have to configure in order to get a bundled html-file that shows the application when I open it?
Thank you
After running "npm run build" on your create-react-app generated code, it displays instructions to help with just this. It says something like:
You may also serve it locally with a static server:
npm install -g pushstate-server
pushstate-server build
The first command, "npm install -g pushstate-server" only needs to be run once, as it installs "pushstate-server" to global NPM. The second command "pushstate-server build" runs the simple static server, pushstate-server, and serves up any content in the "build" folder at http://localhost:9000. You can change the port number if you wish, by adding it at end of command line: "pushstate-server build 1234"
UDPATE: Serverless method...
If your goal is to run the page from the file system, and not from a web server, you'll need to tweak the build/index.html to fix the paths to your JS and CSS (and less importantly, your favicon.ico). The index.html generated by create-react-app expects your JS and CSS to be at "/static/...". However, when you open from the file system, that path is not correct. If you remove the leading forward slash, making the URLs relative, your page will load properly from the file system:
After running "npm run build", open the generated "build/index.html" file. Remove the leading forward slash from "/favicon.ico", "/static/js/main.[random string].js" and "/static/css/main.[random string].css" and save your changes (so that the all read "static/..." and not "/static/..."). Reload/refresh the page in the browser.

Categories

Resources