MOCKSTACKS
EN
Questions And Answers

More Tutorials









React Setting up the project


You need Node Package Manager to install the project dependencies. Download node for your operating system from Nodejs.org. Node Package Manager comes with node.

You can also use Node Version Manager to better manage your node and npm versions. It is great for testing your project on different node versions. However, it is not recommended for production environment.

Once you have installed node on your system, go ahead and install some essential packages to blast off your first React project using Babel and Webpack.
Before we actually start hitting commands in the terminal. Take a look at what Babel and Webpack are used for.

You can start your project by running npm init in your terminal. Follow the initial setup. After that, run following commands in your terminal

Dependencies:


npm install react react-dom --save

Dev Dependecies:


npm install babel-core babel-loader babel-preset-es2015 babel-preset-react babel-preset-stage-0
webpack webpack-dev-server react-hot-loader --save-dev

Optional Dev Dependencies:


npm install eslint eslint-plugin-react babel-eslint --save-dev

Create .babelrc in your project root with following contents:

{
 "presets": ["es2015", "stage-0", "react"]
}

Optionally create .eslintrc in your project root with following contents:

{
 "ecmaFeatures": {
 "jsx": true,
 "modules": true
 },
 "env": {
 "browser": true,
 "node": true
 },
 "parser": "babel-eslint",
 "rules": {
 "quotes": [2, "single"],
 "strict": [2, "never"],
 },
 "plugins": [
 "react"
 ]
}

Create a .gitignore file to prevent uploading generated files to your git repo.

node_modules
npm-debug.log
.DS_Store
dist

Create webpack.config.js file with following minimum contents.

var path = require('path');
var webpack = require('webpack');
module.exports = {
 devtool: 'eval',
 entry: [
 'webpack-dev-server/client?http://localhost:3000',
 'webpack/hot/only-dev-server',
 './src/index'
 ],
 output: {
 path: path.join(__dirname, 'dist'),
 filename: 'bundle.js',
 publicPath: '/static/'
 },
 plugins: [
 new webpack.HotModuleReplacementPlugin()
 ],
 module: {
 loaders: [{
 test: /\.js$/,
 loaders: ['react-hot', 'babel'],
 include: path.join(__dirname, 'src')
 }]
 }
};

And finally, create a sever.js file to be able to run npm start, with following contents:

var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');
new WebpackDevServer(webpack(config), {
 publicPath: config.output.publicPath,
 hot: true,
 historyApiFallback: true
}).listen(3000, 'localhost', function (err, result) {
 if (err) {
 return console.log(err);
 }
 console.log('Serving your awesome project at http://localhost:3000/');
});

Create src/app.js file to see your React project do something.

import React, { Component } from 'react';
export default class App extends Component {
 render() {
 return (
 <h1>Hello, world.</h1>
 );
 }
}

Run node server.js or npm start in the terminal, if you have defined what start stands for in your package.json

Conclusion

In this page (written and validated by ) you learned about React Setting up the project . What's Next? If you are interested in completing React tutorial, your next topic will be learning about: React Using ReactJS with jQuery.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.