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" To fix this, we include @nestjs/schematics in our command yarn dlx -p @nestjs/schematics -p @nestjs/cli nest new project-name We still need to install dependencies cd project-name yarn install I recommend setting Yarn on your project using yarn set version stable 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.* 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" ] And the .dockerignore .yarn .pnp.* dist *.md .git This command yarn workspaces focus --production replicates the old yarn install --production 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 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!

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"
To fix this, we include @nestjs/schematics
in our command
yarn dlx -p @nestjs/schematics -p @nestjs/cli nest new project-name
We still need to install dependencies
cd project-name
yarn install
I recommend setting Yarn on your project using
yarn set version stable
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.*
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" ]
And the .dockerignore
.yarn
.pnp.*
dist
*.md
.git
This command
yarn workspaces focus --production
replicates the old
yarn install --production
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
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!