<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MD Shafayat Islam</title>
    <description>The latest articles on DEV Community by MD Shafayat Islam (@shafayat).</description>
    <link>https://dev.to/shafayat</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2440985%2F7b04553c-bcd6-4403-b857-e192ff4cf1dc.jpg</url>
      <title>DEV Community: MD Shafayat Islam</title>
      <link>https://dev.to/shafayat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shafayat"/>
    <language>en</language>
    <item>
      <title>Express-Typescript-eslint-prettier_setup</title>
      <dc:creator>MD Shafayat Islam</dc:creator>
      <pubDate>Sat, 16 Nov 2024 08:44:39 +0000</pubDate>
      <link>https://dev.to/shafayat/-express-typescript-eslint-prettiersetup-5fhg</link>
      <guid>https://dev.to/shafayat/-express-typescript-eslint-prettiersetup-5fhg</guid>
      <description>&lt;h1&gt;
  
  
  express-typescript-eslint-prettier_setup
&lt;/h1&gt;

&lt;p&gt;This article will help you to instantly setup an express.js server with typescript, eslint and prettier. Follow this article to get the boilerplate, so that you can focus on building your application.&lt;/p&gt;

&lt;p&gt;We will be using yarn package manager in this project, you can also use npm or any other package.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Step 1 — Initialize your project&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;First of all create your project directory, and in your terminal navigate to the folder where you want to create your server and initialze with yarn.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm init -y&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now install Express in your project.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i express&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Step 2 — Adding TypeScript&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;We need to add a typescript package in our project, so that we can use the TypeScript compiler and other related tools.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i -D typescript&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command will add typescript package as a dev-dependency in our project.&lt;/p&gt;

&lt;p&gt;Now, we need to add typescript config file, for that we will use the below given command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tsc --init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will create a &lt;strong&gt;tsconfig.json&lt;/strong&gt; file, with the default compiler configurations shown in the image below.&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;tsconfig.json&lt;/strong&gt; file, remove the comments on the &lt;strong&gt;rootDir&lt;/strong&gt; option and modify it, to set &lt;strong&gt;src&lt;/strong&gt; as root directory for typescript.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"rootDir": "./src",&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Similarly, do this for &lt;strong&gt;outDir&lt;/strong&gt; option as well&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"outDir": "./dist",&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;All &lt;em&gt;.js&lt;/em&gt; files will be created in this &lt;strong&gt;build&lt;/strong&gt; folder after compiling the .&lt;em&gt;ts&lt;/em&gt; files which are inside the &lt;strong&gt;src&lt;/strong&gt; folder.&lt;/p&gt;

&lt;p&gt;Finally, at the end of the &lt;strong&gt;tsconfig.json&lt;/strong&gt; file, add these two options as well. This will tell the compiler which files are needed to be compiled.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// "skipDefaultLibCheck": true,                      /* Skip type checking .d.ts files that are included with TypeScript. */&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"skipLibCheck": true                                 /* Skip type checking all .d.ts files. */&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"include": ["src/**/*.ts"],&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"exclude": ["node_modules"],&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now to check everything is working fine, create a &lt;strong&gt;index.ts&lt;/strong&gt; file inside the &lt;strong&gt;src&lt;/strong&gt; folder. Put some code inside it and then run it in the terminal. You can see that a &lt;strong&gt;index.js&lt;/strong&gt; file is created inside the build folder.&lt;/p&gt;

&lt;p&gt;If you are seeing some red lines in your code, then probably you need to add a package that offers type definitions for the Node.js runtime and its modules.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i -D @types/node&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Step 3 — Adding Eslint&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;For adding eslint, we will install the required packages given below.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i -D eslint @eslint/js @types/eslint__js typescript typescript-eslint&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now make a eslint.config.mjs file in the root of the project director.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx eslint --init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At this point you may see that your version of &lt;code&gt;eslint: "^9.14.0"&lt;/code&gt; has been changed to &lt;code&gt;eslint: "^9.15.0"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;if that happens remove the eslint : &lt;code&gt;npm remove eslint&lt;/code&gt;&lt;br&gt;
Then re-install: &lt;code&gt;npm i -D eslint@version&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Now  add the following code inside it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;ignores&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node_modules&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dist&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;no-unused-vars&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";

/** @type {import('eslint').Linter.Config[]} */
export default [
  { files: ["**/*.{js,mjs,cjs,ts}"] },
  { languageOptions: { globals: globals.node } },
  pluginJs.configs.recommended,
  ...tseslint.configs.recommended,
  {
    ignores: ["node_modules", "dist"],
    rules: {
      "no-unused-vars": "error",
    },
  },
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in the terminal, you can run npm eslint . You can see that eslint is working.&lt;/p&gt;

&lt;p&gt;We can also add scripts for eslint in the &lt;strong&gt;package.json&lt;/strong&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "lint": "eslint src/**/*.ts",
    "lint:fix": "eslint src/**/*.ts --fix"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;strong&gt;Step 4 — Adding Prettier&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Add the prettier package in your project.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i -D --exact prettier&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now create &lt;code&gt;.prettierrc&lt;/code&gt; and &lt;code&gt;.prettierignore&lt;/code&gt; file in the root of your project.&lt;/p&gt;

&lt;p&gt;Include basic configurations for prettier in the &lt;code&gt;.prettierrc&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"semi"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"singleQuote"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, we need to tell prettier which files to not format So inside &lt;code&gt;.prettierignore&lt;/code&gt; include the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dist
coverage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally we can add scripts for prettier as well in the &lt;strong&gt;package.json&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"format": "prettier . --write"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can run yarn format in the terminal to format your project.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
