Setting up Node JS, Express, Prettier, ESLint and Husky Application with Babel and Typescript: Part 1
- Mohammad Abu Mattar
- Backend
- 01 Jul, 2022
- 14 Mins read
Introduction
All code from this tutorial as a complete package is available in this repository. If you find this tutorial helpful, please share it with your friends and colleagues, and make sure to star the repository.
So, in this little tutorial, I’ll explain how to set up babel for a basic NodeJS Express, and typescript application so that we may utilize the most recent ES6 syntax in it.
What is TypeScript?
TypeScript is a superset of JavaScript that mainly offers classes, interfaces, and optional static typing. The ability to enable IDEs to give a richer environment for seeing typical mistakes as you enter the code is one of the major advantages.
- JavaScript and More: TypeScript adds additional syntax to JavaScript to support a tighter integration with your editor. Catch errors early in your editor.
- A Result You Can Trust: TypeScript code converts to JavaScript, which runs anywhere JavaScript runs: In a browser, on Node.js or Deno and in your apps.
- Safety at Scale: TypeScript understands JavaScript and uses type inference to give you great tooling without additional code.
What is Babel?
Babel Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments. Here are the main things Babel can do for you:
- Transform syntax
- Polyfill features that are missing in your target environment (through a third-party polyfill such as core-js)
- Source code transformations (codemods)
Project Setup
We’ll begin by creating a new directory called template-express-typescript-blueprint
and then we’ll create a new package.json file. We’re going to be using yarn for this example, but you could just as easily use NPM if you choose, but yarn is a lot more convenient.
Now we’ll connect to our new project with git.
A new Git repository is created with the git init command. It may be used to start a fresh, empty repository or convert an existing, unversioned project to a Git repository. This is often the first command you’ll perform in a new project because the majority of additional Git commands are not accessible outside of an initialized repository.
Now we’ll connect to our new project with github, creating a new empty repository, after we’ve created a new directory called template-express-typescript-blueprint
.
Engine Locking
The same Node engine and package management that we use should be available to all developers working on this project. We create two new files in order to achieve that:
.nvmrc
: Will disclose to other project users the Node version that is being utilized..npmrc
: reveals to other project users the package manager being used.
.nvmrc
is a file that is used to specify the Node version that is being used.
.nvmrc
.npmrc
is a file that is used to specify the package manager that is being used.
.npmrc
Now we’ll add few things to our package.json
file.
package.json
Notably, the usage of engine-strict
said nothing about yarn in particular; we handle that in packages.json
:
open packages.json
add the engines:
Installing and Configuring TypeScript
TypeScript is available as a package in the yarn registry. We can install it with the following command to install it as a dev dependency:
Now that TypeScript is installed in your project, we can initialize the configuration file with the following command:
Now we can start config the typescript configuration file.
tsconfig.json
Installing and Configuring Babel
In order to set up babel in the project, we must first install three main packages.
babel-core
: The primary package for running any babel setup or configuration is babel-core.babel-node
: Any version of ES may be converted to ordinary JavaScript using the babel-node library.babel-preset-env
: This package gives us access to forthcoming functionalities thatnode.js
does not yet comprehend. New features are constantly being developed, thus it will probably take some time for NodeJS to incorporate them.
After that, we need to create a file called .babelrc
in the project’s root directory, and we paste the following block of code there.
.babelrc
Add the following line to the package.json
file to compile, and build the code with babel:
Now we need to add .gitignore
file to the project, and add the following line to it:
The .gitignore
file tells Git which files to ignore when committing your project to the GitHub repository. gitignore is located in the root directory of your repo.
.gitignore
Code Formatting and Quality Tools
We will be using two tools in order to establish a standard that will be utilized by all project participants to maintain consistency in the coding style and the use of fundamental best practices:
- Prettier: A tool that will help us to format our code consistently.
- ESLint: A tool that will help us to enforce a consistent coding style.
Installing and Configuring Prettier
Prettier will handle the automated file formatting for us. Add it to the project right now.
Additionally, I advise getting the Prettier VS Code extension so that you may avoid using the command line tool and have VS Code take care of the file formatting for you. It’s still required to include it here even when it’s installed and set up in your project since VSCode will utilize your project’s settings.
We’ll create two files in the root:
.prettierrc
: This file will contain the configuration for prettier..prettierignore
: This file will contain the list of files that should be ignored by prettier.
.prettierrc
.prettierignore
I’ve listed the folders in that file that I don’t want Prettier to waste any time working on. If you’d want to disregard specific file types in groups, you may also use patterns like *.html.
Now we add a new script to package.json
so we can run Prettier:
package.json
You can now run yarn prettier
to format all files in the project, or yarn prettier:check
to check if all files are formatted correctly.
to automatically format, repair, and save all files in your project that you haven’t ignored. My formatter updated around 7 files by default. The source control tab on the left of VS Code has a list of altered files where you may find them.
Installing and Configuring ESLint
We’ll begin with ESLint, which is a tool that will help us to enforce a consistent coding style, at first need to install the dependencies.
We’ll create two files in the root:
.eslintrc
: This file will contain the configuration for ESLint..eslintignore
: This file will contain the list of files that should be ignored by ESLint.
.eslintrc
.eslintignore
Now we add a new script to package.json
so we can run ESLint:
package.json
You can test out your config by running:
You can now run yarn lint
to format all files in the project, or yarn lint:check
to check if all files are formatted correctly.
Git Hooks
Before moving on to component development, there is one more section on configuration. If you want to expand on this project in the future, especially with a team of other developers, keep in mind that you’ll want it to be as stable as possible. To get it right from the beginning is time well spent.
We’re going to use a program called Husky.
Installing and Configuring Husky
Husky is a tool for executing scripts at various git stages, such as add, commit, push, etc. We would like to be able to specify requirements and, provided our project is of acceptable quality, only enable actions like commit and push to proceed if our code satisfies those requirements.
To install Husky run
A .husky
directory will be created in your project by the second command. Your hooks will be located here. As it is meant for other developers as well as yourself, make sure this directory is included in your code repository.
Add the following script to your package.json
file:
package.json
This will ensure Husky gets installed automatically when other developers run the project.
To create a hook run:
The aforementioned states that the yarn lint
script must run and be successful before our commit may be successful. Success here refers to the absence of mistakes. You will be able to get warnings (remember in the ESLint config a setting of 1 is a warning and 2 is an error in case you want to adjust settings).
We’re going to add another one:
This makes sure that we can’t push to the remote repository until our code has built correctly. That sounds like a very acceptable requirement, don’t you think? By making this adjustment and attempting to push, feel free to test it.
Installing and Configuring Commitlint
Finally, we’ll add one more tool. Let’s make sure that everyone on the team is adhering to them as well (including ourselves! ), since we have been using a uniform format for all of our commit messages so far. For our commit messages, we may add a linter.
We will configure it using a set of common defaults, but since I occasionally forget what prefixes are available, I like to explicitly provide that list in a commitlint.config.js
file:
commitlint.config.js
Afterward, use Husky to enable commitlint by using:
now push your changes to the remote repository and you’ll be able to commit with a valid commit message.
now we need to make sure that we can push to the remote repository, we forgot to add the build
command to the .husky/pre-push
file.
Create somple setup express, typescript and babel application
Create a file structure like this:
start to add express and typescript dependencies:
New we’ll add a new package:
compression
: YourNode.js
app’s main file contains middleware forcompression
. GZIP, which supports a variety ofcompression
techniques, will then be enabled. Your JSON response and any static file replies will be smaller as a result.
cookie-parser
: YourNode.js
app’s main file contains middleware forcookie-parser
. This middleware will parse the cookies in the request and set them as properties of the request object.
core-js
: YourNode.js
app’s main file contains middleware forcore-js
. This middleware will add the necessary polyfills to your application.
cors
: YourNode.js
app’s main file contains middleware forcors
. This middleware will add the necessary headers to your application.
helmet
: YourNode.js
app’s main file contains middleware forhelmet
. This middleware will add security headers to your application.
regenerator-runtime
: YourNode.js
app’s main file contains middleware forregenerator-runtime
. This middleware will add the necessary polyfills to your application.
after that we need to add the type for the dependencies:
now we’ll start with create constants and we’ll add new things after that:
api.constant.ts
http.code.constant.ts
http.reason.constant.ts
message.constant.ts
utils/exception/http.exception.ts
error.middleware.ts
controller.interface.ts
index.ts
www.ts
To run the app, and start tarcking the server, with the changes, we need to add new dependency.
Concurrently: is a tool to run multiple tasks at the same time.
Then, we’ll add the following command to scripts section of package.json:
New you can run the application with yarn start or yarn dev, and you can also run the application with yarn build to create a production version.
Summary
Finally, after compilation, we can now need to deploy the compiled version in the NodeJS production server.
All code from this tutorial as a complete package is available in this repository.