Use this template to create a React/Redux app with Node backend (using Babel/Webpack for client compatibility)
- Copy/clone entire contents of this project to your new (empty) project folder (make sure to include .babelrc; a base .gitignore file is also included if desired)
- Ensure you are using npm v6.14.x or higher (
npm -v
) and node v13.12.x or higher (node -v
) - Navigate to the root directory of your project and run
npm init
and follow the prompts.- When asked for an entry point, make sure to enter
index.js
.
- When asked for an entry point, make sure to enter
- In the root directory of your new project, run the following to install Babel, React, Webpack, and other tools:
npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/preset-react
npm install react react-dom
npm install --save-dev webpack webpack-cli webpack-dev-server style-loader css-loader babel-loader
npm install redux react-redux reselect
- optional (to provide support for local persistent storage):
npm install redux-persist
- NOTE: if you skip this step, see imports and comments in both
src/store.js
andsrc/index.js
for some boilerplate that needs to be removed.
- optional (to provide support for dispatching async actions):
npm install redux-thunk redux-devtools-extension @babel/runtime
npm install --save-dev @babel/plugin-transform-runtime
- NOTE: if you skip this step, remove
"@babel/plugin-transform-runtime"
from theplugins
array in.babelrc
, and see imports and comments insrc/store.js
for some additional boilerplate that needs to be removed.
- optional (to enable hot loading):
npm install --save-dev react-hot-loader
- NOTE: if you skip this step, replace the
default export
insrc/App.js
withexport default App
, and omit the--hot
option from your launch command in steps 5 and 6, below.
- Finally, run
npx webpack serve --hot --mode development
to launch the app in development mode (if you didn't installreact-hot-loader
then omit--hot
from the launch command). You should now be able to load the contents ofApp.js
at http://localhost:3000/. It simply renders "Hello, World!" - If you would like a shorter alias for launching the app, you can insert the following into the
scripts
object inpackage.json
:"dev": "npx webpack serve --hot --mode development",
(if you didn't installreact-hot-loader
then omit--hot
from this line). Now you can simply runnpm run dev
as an alias for starting the dev build. - You may also like to see the dist folder which is typically suppressed when running webpack in development mode. You can add another alias,
"build": "npx webpack --mode development",
, after the"dev": ...
alias added in step 6. Runningnpm run build
will now populate the dist folder in you project root.
That's a lot of frontend power, but here are some other recommendations for npm
packages that can be useful to install:
styled-components
: for dynamically styling componentsmocha
andchai
: for testing- Install these alongside
@babel/register
, using the--save-dev
flag for all 3 packages. - To test async Thunks, also install
sinon
,node-fetch
, andfetch-mock
, also using the--save-dev
flag. - For copy/paste convenience, here's the entire installation:
npm install --save-dev mocha chai @babel/register sinon node-fetch fetch-mock
- To optimize
npm run test
, you'll want to assign thetest
alias inpackage.json
to something like the following:"mocha \"src/**/*.test.js\" --require @babel/register --recursive"
. This will allow you to writedescribe(...)
style tests in*.test.js
files anywhere in your project without worrying about exports.
- Install these alongside
jest
: an alternative tomocha
andchai
for testing- Also needs
babel
support and a differentnpm run test
alias. See: https://redux.js.org/recipes/writing-tests for more info.
- Also needs