React Tooling Part 1
Now we will deep dive into tooling around tooling around React and modern frontend development in general.
NodeJS
NodeJS is a Javascript runtime. It enables you to run javascript outside browser, in your laptop and server. NodeJS is the main reason that makes Javascript so popular today, as it makes Javascript the single programming language that can supports from the frontend UI to the backend services like web server and database.
Technically, React does not need NodeJS (which I have demonstrated in previous section). However, NodeJS is important to React because it is the environment that enables the tooling that React depends heavily e.g. package management, bundling, transpiling, formating, and linting.
npm
- npm is the package manager for NodeJS.
- It hosts packages for NodeJS and all the packages for front-end. npm has a command-line tool, called
npm
as well. The command-line tool is prepackaged together with NodeJS so you usually download them together. npm
allows you to install code from npm registry which are the open-source projects that you can use in your project. When you runnpm install react
, it will download the latest version of React in npm registry to your project. (Don’t do this yet)
Creating a project with npm
In order to start an npm project, run npm init
at the root of your project folder. It will ask you a few questions. If you don’t know the answer or don’t care, just hit Enter, you can always update it later by editing package.json
file.
Exercise
- create a new folder and call it
react-movie-app
- open the
react-movie-app
folder - In your command line, enter
npm init
and answer the questions. A file with namepackage.json
should be generated in the folder. You can open it with your editor - it is a JSON file that captures what you’ve answered, thus you can edit this file as you wish. - move the
index.html
file into thereact-movie-app
folder - create a file alongside the html file and call it
script.js
- cut and paste our code within
script
tag intoscript.js
- remove the
script
tag in our html file and replace it with<script src="script.js"></script>
- open the
index.html
file with browser and ensure it still works.
Commit: 010-initial-project
prettier
Prettier is an opinionated code formatter that removes worries about the style of your code. It will takes your code and reformat it based on predefined styles. Since you no longer has control of the style of your code, your code is always consistent, as is the code from the rest of your team.
To use prettier to format your code, run the following command npx prettier --write script.js
. Prettier will format code of script.js
. Try to modify script.js
and make it look ugly and run npx prettier --write script.js
again - prettier will make the code back to its format.
Prettier is great to use with VS code, download this extension.
npm scripts
It can be painful to remember CLI commands to run for your project. npm allows you to define common commands that you run for your project and list it in package.json
, so you don’t need to remember it.
Let’s configure npm scripts for the prettier formatting above:
- Install prettier in your project by running
npm install -D prettier
. The-D
flag implies prettier will be installed asdevDependencies
. - Let’s customize prettier by adding a
.prettierrc
file with the following content:{ "singleQuote": true }
- Add the
format
script in yourpackage.json
:{ "name": "react-movie-app", ... "scripts": { "format": "prettier --write script.js" } }
Now you can invoke prettier by running npm run format
.
Exercise
- Install prettier and configure format script based on the instruction above.
- (Optional) Configure prettier plugin if you’re using VS code.
Commit: 020-prettier
ESLint
- ESLint is a linter that enforce some code styles that more to usage than to code styles, e.g. never use
with
or never has unused parameters in your function. - To use ESLint in your project:
- install the packages as devDependencies:
npm install -D eslint eslint-config-prettier
- create a file and call it
.eslintrc
with the following content:{ "extends": ["eslint:recommended", "prettier", "prettier/react"], "plugins": [], "parserOptions": { "ecmaVersion": 2016, "sourceType": "module", "ecmaFeatures": { "jsx": true } }, "env": { "es6": true, "browser": true, "node": true } }
- Add the following to npm scripts:
"lint": "eslint **/*.{js,jsx} --quiet"
- install the packages as devDependencies:
- Now when you run
npm run lint
, eslint will output some error messages. For now just ignore the error, we will fix it later. - Similar to prettier, ESLint is great integration with VS Code as well. Download this extensions.
Exercise
- Install ESLint and configure lint script based on the instruction above.
- (Optional) Configure ESLint plugin if you’re using VS code.
Commit: 021-eslint