React npm run build, what is that, why we need that? - node.js

Currently I made a simple app with react, it works perfectly and I use react-scripts. I installed React by following the Facebook github page, and it written that "When you're ready to deploy to production, running npm run build will create an optimized build of your app in the build folder. ". After I press npm run build, a folder build created, but I do not know what to do with this folder. I tried some method like, move all folders except build and npm run, but it did not work. Anyone can explain me what is this folder "build" ? Thank you

npm run build builds the app for production to the build folder.
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.
npm run build creates a build directory with a production build of your app. Set up your favourite HTTP server so that a visitor to your site is served index.html, and requests to static paths like /static/js/main..js are served with the contents of the /static/js/main..js file.

Related

index.html not found while deploying MERN app to render.com

I was trying to deploy my MERN based E-commerce website on render.com, after the render terminal shows the build was successful the the webpage shows the error as,
{“message”:“ENOENT: no such file or directory, stat ‘/opt/render/project/src/frontend/build/index.html’”,“stack”:null}
I’m a complete beginner to render and MERN too,
I have no bulid folder in my local
then I tried to create another test app by npx create-react-app test to check if build folder is actually present in there or not, But it is not there, I’m totally in Confused now…
I’m Giving my repo here → stunning spark
I just want to have clear answers for my questions(Please!!!)
Things I want to change in my directory to deploy and host my app Successfully.
Things Need to be configured in the render’s settings
Thanks in Advance!!!
I just want to deploy and host my application on render.com
Just run npm run build into your frontend folder and do check you have removed the "build" keyword from the .gitignore file because it won't let you push the build folder to GitHub and you are good to go.

how to deploy a reacjs application in linux?

newbie in node and reactjs. I created a sample application with create-react-app. I ran npm run build to build the project and once successfully built saw following .
what does the static server mean here. I am planning to build a app, not just static web pages. will these steps to build and deploy work, if I copy the build folder to my development box and ran the following commands.
do i need to install any web server for it work in a linux dev box? how does the deployment work in nodejs/linux work, compared to like in microsoft box , you have to install IIS web server then deploy the website under it.
The build folder is ready to be deployed.
You may serve it with a static server:
npm install -g serve
serve -s build
In the developing phase you can use npm start to start a dev server that auto reloads on changes. (Pretty neat).
When you want to host your app you should use the npm run build command and deploy it as a static website. You can use a basic Apache Server for that. Your build folder should contain an index.html and various js files that will be loaded when a user requests your website.
Voilá you hosted your react app ;)

deploying production mode with Vue-cli created project

Bit of a n00b question:
I created project with vue-cli using webpack.
On my windows machine I run "npm un dev" and I get a frontend server with HMR and so on.
Now I want to deploy my app to a production machine - ubuntu on DigitalOcean.
What are the steps I must take? I'm not very familiar with the logic of how it's supposed to work. If my ubuntu machine has NODE_ENV set to production, it won't install any of the devDependancies and i'm not able to build anything. So I guess I'll have to change that? If yes then it doesn't make any sense since it's a production machine.
And do I have to create another node/express server to serve index.html? Won't it supposed to work out-of-the-box somehow?
Thanks :)
TL;DR Build on your local machine and everything you need will be outputted in the ./dist/ directory, just copy the contents over to the webroot on your production server and you're good to go.
The webpack template handles most of the stuff for you.
Step you need to take to release:
Run npm run build on your local machine
Copy the contents of the generated ./dist/ directory to your server webroot
That's it!
When you run npm run build, the pre-configured build script sets the node environment to production, and builds with only the stuff that should be in production, it also optimizes the code and removes debug capabilities. When it comes to dependencies webpack takes care of that and includes them in the generated javascript files located in the ./dist/js/, so you need not concern yourself with copying over the node_modules/ directory either.
It also copies over everything in your static directory and src/assets directory to the ./dist/ directory to be prepare for a release. And resolves all the references to the new path generated by webpack.
The production server should not be concerned with building the vue app, run the build command on your local machine to keep dev dependencies away from your production server. I recommend against installing webpack and other dev tools on your production server. It just pollutes the server with things not needed there.
Some development tools could potentially produce alot of issues on production servers. So best practice is to never install them.
You could optionally create your own release script that uses ftp or rsync, whatver you prefer to copy everything in the ./dist/ directory to the production server webroot. This could be a script in bash, if on windows, run it in git bash or something similar for example.
Hope that cleared things up, congrats on your first vue release!

Which files from Node.js app must be uploaded to a web-hosting?

Perhaps this is a silly question. It came out while I was learning how to set up a Node.js application for production on Ubuntu and digital ocean.
Let's say I have a simple data visualization app made in Node.js, using node modules such as express, page, axios, yo-yo, and browserify to compile my files.
I want to upload my app to a webhost that already exists.
This is the structure's app:
node_modules
public (app.js and app.css)
src (header, home, and footer folders)
views (index.pug file)
gulpfile.js
index.scss
package.json
server.js
Which files I need to upload in order to see my app as I see it in localhost?
You need to upload everything.
What Maximelian says is true if you're going to run npm install again on your server. The standard way of doing this is sync the project using git (you can find a .gitignore template for node.js here).
Once setup you'd do something like this on the server after making the commit locally, and pushing to your remote git repo:
git pull
npm install
npm start
If you were to just ftp the full working project including node_modules it would work just by running npm start. But the above method is what I'd recommend.
If I remember correctly everything except node_modules, if you did not customized them. (rewrite some behavior after module installation)

Have the server build VS. check in & push a locally built app

What are some pro/cons to pushing built code vs. having the server build it?
This is a general question, but here's my specific scenario to illustrate what I'm talking about:
On Heroku I've got a React app that has a simple express server to do OAuth. Currently, I have a postinstall hook in my package.json that runs a webpack production config to do some extract-text stuff and create a dist/ directory with my bundled, uglifyied code. To get all of this to run on Heroku I had to mark pretty much all of my dependencies as 'dependencies' instead of 'devDependencies'.
I know it's a bad practice to check my dist/ into git but it would save me from having to install a dozen plus node_modules on the server. Any thoughts?

Resources