This is the easiest way to get started
npx create-react-app my-app
cd my-app
npm start
Below is an alternative, but it's a lot more involved.
mkdir my-app // create directory
cd my-app
npm init -y //create package.json
npm install react react-dom //install react and react-dom
touch index.js index.css
You can add your code to index.js. It will looks something like this
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
class App extends React.Component{
render(){
return(
<div>Hello World</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('app'))
After that you'll need to get your babel
npm install --save-dev @babel/core @babel/preset-env @babel/preset-react webpack webpack-cli webpack-dev-server babel-loader css-loader style-loader html-webpack-plugin
touch webpack.config.js
Configure your webpack
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry : './my-app/index.js',
output : {
path : path.resolve(__dirname , 'dist'),
filename: 'index_bundle.js'
},
module : {
rules : [
{test : /\.(js)$/, use:'babel-loader'},
{test : /\.css$/, use:['style-loader', 'css-loader']}
]
},
mode:'development',
plugins : [
new HtmlWebpackPlugin ({
template : 'my-app/index.html'
})
]
}
Add babel presets and npm command to build (build) and run (dev) your code to package.json
"main": "index.js",
"babel": {
"presets": ["@babel/preset-env", "@babel/preset-react"]
},
"scripts": {
"build": "webpack",
"dev": "webpack-dev-server --open"
}