DEV Community

Amir Alam
Amir Alam

Posted on

Revolutionize Your Nest.js Compilation Time: Boosting Speed by 20x with SWC!

Are you tired of waiting ages for your Nest.js applications to compile? Say goodbye to sluggish build times and embrace a lightning-fast development experience with SWC. In this guide, we'll explore how you can supercharge your Nest.js compilation process and achieve a jaw-dropping 20x speed improvement. Buckle up and get ready to witness a remarkable transformation in your development workflow as we unlock the immense power of SWC. Say hello to faster builds and increased productivity, all thanks to this game-changing tool. Let's dive in and revolutionize your Nest.js compilation time like never before!

Existing Compiler

Nest.js utilize the TypeScript compiler (tsc) as the default compiler in standard mode and webpack in monorepo mode. However, the tsc compiler is known to thave relatively slower compilation speed. In comparison, SWC, a Rust-based tool, offers a faster alternative for compiling and bundling Typescript and javascript.

Getting started

After generating a new Nest.js project using the Nest CLI, the project is set up to use the default TypeScript compiler (tsc). However, to leverage the benefits of SWC, let's migrate it.

Step 1

Install Required dependencies

  • @swc/core : SWC core compiler

npm --save-dev @swc/core

  • @swc/cli : CLI to interact with swc core

npm --save-dev @swc/cli

  • nodemon : for keeping process alive and watching files for changes

npm --save-dev nodemon

  • concurrently : for running commands concurrently

npm --save-dev concurrently

Step 2

Setting up swc config

create a .swcrc file in your roor directory and paste this configuration

  "$schema": "",
  "sourceMaps": true,
  "module": {
    "type": "commonjs"
  "jsc": {
    "target": "es2017",
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "dynamicImport": true
    "transform": {
      "legacyDecorator": true,
      "decoratorMetadata": true
    "keepClassNames": true,
    "baseUrl": "./"
  "minify": false
Enter fullscreen mode Exit fullscreen mode

Step 3

Now we have setup the SWC, let's test it

Compile Nest js

npx swc --out-dir dist -w src

this command will watch the files and immediately compile it to dist folder,
Run Nest js

node dist/main.js

Now you should see the nest js app running.

Step 4

In the previous step, we successfully ran our new project with SWC compilation. However, writing the long command every time can become cumbersome. To simplify this process, we can shorten the command by adding it as a script in the package.json file.

Add this to root package.json

 "scripts": {
    "build:swc": "npx swc --out-dir dist -w src",
    "start:swc": "nodemon dist/main",
    "dev": "concurrently   \"npm run build:swc\" \"npm run start:swc\" ",
Enter fullscreen mode Exit fullscreen mode

Now test it by writing

npm run dev

and you should see your app running quickly.

End Note

Note: SWC isn't compatible with NestJS CLI plugins.
If you're using a plugin, you should stick to tsc or webpack (nest build and nest start).

Oldest comments (0)