DEV Community

Cover image for NestJs + Yarn v4: Installation and deployment
5inchiRoca
5inchiRoca

Posted on

1

NestJs + Yarn v4: Installation and deployment

I encountered issues installing NestJS with Yarn v4, such as missing dependencies and CLI errors. I searched for this specific issue but couldn’t find an exact match, so I decided to write this post

Tech used:

  • Node v22
  • Yarn v4

Installing NestJS with Yarn v4

We need to use @nestjs/schematics otherwise it will throw an error like this

Error: Collection "@nestjs/schematics" cannot be resolved.

Failed to execute command: node @nestjs/schematics:application --name=project-name-2 --directory=undefined --no-dry-run --no-skip-git --no-strict --package-manager="yarn" --collection="@nestjs/schematics" --language="ts"
Enter fullscreen mode Exit fullscreen mode

To fix this, we include @nestjs/schematics in our command

yarn dlx -p @nestjs/schematics -p @nestjs/cli nest new project-name
Enter fullscreen mode Exit fullscreen mode

We still need to install dependencies

cd project-name
yarn install
Enter fullscreen mode Exit fullscreen mode

I recommend setting Yarn on your project using

yarn set version stable
Enter fullscreen mode Exit fullscreen mode

This will update our package.json so in the next step using corepack enable will automatically use the Yarn version defined on our package.json

By default, Yarn v4 uses Plug’n’Play (PnP), which creates .pnp.* files. These files should not be committed, so we add them to .gitignore

/.yarn
.pnp.*
Enter fullscreen mode Exit fullscreen mode

Deployment

This is the Dockerfile I created

FROM node:22-alpine AS builder

WORKDIR /build

COPY package.json yarn.lock .

RUN corepack enable \
&& yarn install

COPY . .

RUN yarn build

FROM node:22-alpine

WORKDIR /app

COPY package.json yarn.lock .

RUN corepack enable \
&& yarn workspaces focus --production

COPY --from=builder /build/dist dist

EXPOSE 80

CMD [ "yarn", "start:prod" ]
Enter fullscreen mode Exit fullscreen mode

And the .dockerignore

.yarn
.pnp.*
dist
*.md
.git
Enter fullscreen mode Exit fullscreen mode

This command

yarn workspaces focus --production
Enter fullscreen mode Exit fullscreen mode

replicates the old

yarn install --production
Enter fullscreen mode Exit fullscreen mode

You will need to make some adjustments based on your project needs. We don't need to copy the .yarn directory from the builder stage, since Yarn uses global cache. The command above creates a .yarn directory with production-only dependencies inside /app directory as well as the default global cache folder for the current stage. We can see the path of this cache folder using the command

yarn config get cacheFolder
Enter fullscreen mode Exit fullscreen mode

This setup ensures a smooth installation of NestJS with Yarn v4 and provides an optimized Dockerfile for production deployments. Let me know in the comments if you have any questions or improvements!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay