[Nest.js] Few thoughts on setting up Nest.js SWC with Docker

James ZhangJames Zhang
2 min read

The official doc (https://docs.nestjs.com/recipes/swc) goes through SWC setup in Nest.js pretty well. But usually when we setup our Nest.js project with Docker, I try to get SWC working and here are thoughts around it. SWC is a little tricky because its package (in Rust) requires platform-native building.

As an example, I have a similar setup in this overall repo https://github.com/chat-stack/chat-stack using Nest.js and SWC. I will use code pointers from there.

When I install SWC it is in my Mac environment, and it works. But in Docker it doesn't. In Docker it actually needs the Docker's OS's SWC package.

My Docker runs on Alpine linux (https://github.com/chat-stack/chat-stack/blob/main/docker-compose.yml#L17)

Here is my "hack" fix:

  1. Add 2 more pkgs to dev dependencies in package.json
{
  "devDependencies": {
    "@swc/core-linux-arm64-musl": "^1.3.95",
    "@swc/core-linux-x64-musl": "^1.3.95",
  }
}

This may be redundant but adding it for both arch will work to run my Docker in both arch. These two works for Alpine Linux in my Docker.

  1. Add optional dependencies and it will automatically figure out when I install it outside docker
{
  "optionalDependencies": {
    "@swc/core-darwin-arm64": "^1.3.95",
    "@swc/core-darwin-x64": "^1.3.95",
    "@swc/core-linux-arm-gnueabihf": "^1.3.95",
    "@swc/core-linux-arm64-gnu": "^1.3.95",
    "@swc/core-linux-x64-gnu": "^1.3.95",
    "@swc/core-win32-arm64-msvc": "^1.3.95",
    "@swc/core-win32-ia32-msvc": "^1.3.95",
    "@swc/core-win32-x64-msvc": "^1.3.95"
  }
}

Other points worth attention

0
Subscribe to my newsletter

Read articles from James Zhang directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

James Zhang
James Zhang

Founding Engineer at Bazar (General Catalyst backed), Ex Meta Ads MLE, Ex Activision Blizzard Intern