Академический Документы
Профессиональный Документы
Культура Документы
HOME LEARN WEB DEVELOPMENT WEB DEV COURSES WRITE FOR US FREE $10 IN BITCOIN GET WEEKLY EMAILS
Setting up a React project from scratch can be a daunting task for beginners.
Lots of npm modules to install, lots of con guration and other tasks.
There are lots of react project seeds we can use in place of starting a project
from scratch but then they come with a lot of boilerplate hard to make sense
for a beginner who is just starting out.
I was rebuilding my personal website in react and come with the idea to share
with you all how I will be building that project from scratch.
let’s start by creating a node project. In your terminal type these command.
1 mkdir reactProject
2 cd reactProject
3 npm init // start a node project
You will be prompted to input information about your project. You can just
press enter for every choice. if you wish to add information about your project
go ahead.
We are going to use webpack for bundling our code into one js le. So our
Next step is to install webpack and webpack-dev-server .
The webpack lib is used to bundle and output out js code in the location we
speci ed. The webpack-dev-server lib will help us to run development server
which will give use bene ts like hmr & live reload .
Now we will be needing babel to convert our es6 code to es5 browser
understandable code. Let’s install babel
We have installed babel and its supporting libs. Let’s con gure our react app
so it will make use of babel transpile our code into es5. Inside your
package.json write
Basically, we told our app to make use of babel presets. By adding presets
now babel can understand our es6/react code and can convert it to es5.
We also installed a babel plugin which can understand the new rest/spread
operator syntax.
Let’s move on to webpack . As you already know we have to con gure the
webpack to make use of our babel loader to transpile and output our
bundle.js le.
1 mkdir webpack
2 cd webpack
3 touch webpack.dev.config.js
let’s add some loaders which will be responsible to bundle our source les.
As you can see we setup babel-loader for loading js/jsx les and used less-
Okay now let’s tell our webpack con g where to output our bundle.js le
1 output: {
2 path: __dirname + '/dist',
3 filename: 'bundle.js'
4 }
Our last step to end our webpack con guration will be providing the options
for our dev server.
1 devServer: {
2 contentBase: __dirname + '/dist',
3 historyApiFallback: true
4 }
Here is what our whole webpack.dev.config.js looks like I also added the
parentDir variable to make referencing our parent directory easy.
Now let’s create our index.html . Inside your project root dir create an
index.html le.
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title> Ideal React Seed </title>
5 </head>
6 <body>
7 <div id="app"></div>
8 <script type="text/javascript" src="/bundle.js"></script>
9 </body>
10 </html>
Next step will be building our index.js le which will be the entry point for
our webpack con guration. Inside you dir create index.js
We imported react & react-dom from their respective packages. Our main
app will be inside the ./containers/App . Atlast we rendered our app into
our main html node which is <div id="app"></div> .
Now let’s quickly create an App.js to hold our whole application. Inside your
app directory create a folder named containers and inside that App.js
1 mkdir containers
2 cd containers
3 touch App.js
We create a simple component which will display This is my new react app
when rendered.
but before that let’s add a npm script which will help us to launch the dev
server by just typing npm run dev in the console.
1 "scripts": {
2 "test": "echo \"Error: no test specified\" && exit 1",
3 "dev": "./node_modules/.bin/webpack‐dev‐server ‐‐config ./webpack/webpack.dev.config.js"
4 }
in your console type npm run dev and goto http://localhost:8080 you will
see your app working perfectly.
App running perfectly :)
This is it for now. You can play with this setup for a while Here is the
second part where I will be using redux & react-router in this project.
Also there will be third a part as well where I will show you how to use
webpack for deploying this project from development to production.
Until then share this tutorial with everyone who can nd this tutorial
helpful.
. . .
Hi, My name is Manoj Singh Negi. I am a Javascript Developer and writer follow
me at Twitter or Github.
3. Be an Artist.
Peace.
2K 25
Responses
Write a response…
Stanley Sathler
Dec 16, 2017
One of the best topics about setting up a React application that I have ever
seen so far.
Clear, detailed, with short steps. Not tons of con gurations, just the basic.
34 1 response
1
Conversation between Karthik P and Manoj Singh Negi.
Karthik P
Jan 13
9 1 response
15
Gricel Sepúlveda
Jan 30
Hi, I was following all steps and when I type “npm run dev” the browser
doesn’t start the proyect automatically, when I go manually to
http://localhost:8080 I get the following message:
“CANNOT GET /”
My project repository:
https://github.com/gricelsepulveda/react-meeta
2 1 response
devServer: {
contentBase: __dirname + '/dist',
historyApiFallback: true
}
But in the article the con guration of webpack is something like this
devServer: {
contentBase: parentDir,
historyApiFallback: true
}
Read more…
Stephanie K
Jan 4
Thanks for the article. Small question: When adding the dev script, why are
you refering to “./node_modules/.bin/webpack-dev-server” instead of just
“webpack-dev-server”?
12 1 response
KuanYu Chu
Jan 28
package.json
Isn’t it .babelrc
1 1 response
KuanYu Chu You can either add this in your .babelrc or package.json both will
work.
Luis Garcia
Dec 13, 2017
10 1 response
Manohar Prabhu
Nov 17, 2017
Very good article, I am struggling with React Project setup, this article helps
me to setup the react and it is up and running smoothly. Thanks for you
article.
13 1 response
fails,
1 response
Tacy Nathan
Nov 18, 2017 · 1 min read
Pulled your code from github and bang! The same error.
1 response
codeKameleon
Jan 12
1 response
codeKameleon The bundle.js le will be created when you will generate the
build using this command