npm install -g babel webpack webpack-dev-server
mkdir <project-folder> && cd <project-folder>
npm init
npm install --save react react-dom
npm install --save-dev babel-core babel-loader babel-preset-es2015 babel-preset-react webpack webpack-dev-server
mkdir src src/components public
touch public/index.html src/components/App.js src/index.js webpack.config.js
in webpack.config.js
module.exports = {
entry: './src/index.js',
output: {
path: __dirname + '/public/',
filename: 'bundle.js'
},
devServer: {
inline: true,
port: 7777,
contentBase: __dirname + '/public/'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/,
query: {
cacheDirectory: true,
presets: ['es2015', 'react']
}
}
]
}
};
in package.json
"scripts": {
"start": "webpack-dev-server --hot --host 0.0.0.0"
}
in public/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>React App</title>
</head>
<body>
<div id="root"></div>
<script src="bundle.js"></script>
</body>
</html>
in src/components/App.js
It's React's Naming Convention to set component's and file's first letter to Capital letter.
import React from 'react';
class App extends React.Component {
render(){
return (
<h1>Hello, React!</h1>
);
}
}
export default App;
in src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
const rootElement = document.getElementById('root');
ReactDOM.render(<App />, rootElement);
npm start
Open browser, go to localhost:7777.