DEV Community

Micael Levi L. C.
Micael Levi L. C.

Posted on

NestJS tip: how to try any official sample in seconds

Let's say you want to run one of the official NestJS samples, the ones under the sample folder of the nestjs/nest repository, to see how some feature looks like in a real app. But you don't want to clone the whole framework just for that!

What?

Just run this:

## Node.js v22+ is required
npx try-nest@latest
Enter fullscreen mode Exit fullscreen mode

Pick a sample, pick a directory, and optionally install the dependencies with npm, pnpm, yarn or bun. Each sample comes with its own description, so you don't need to guess what 18-context is about:

? Which sample would you like to try?
❯ 01-cats-app               Guards, pipes, interceptors, filters and middleware in a REST API
  02-gateways               WebSocket gateway with Socket.IO and an optional Redis adapter
  03-microservices          Hybrid HTTP and TCP microservice with request-response messaging
  04-grpc                   Hybrid HTTP and gRPC microservice with streaming and server reflection
  05-sql-typeorm            TypeORM integration with MySQL via @nestjs/typeorm
  06-mongoose               Mongoose integration with MongoDB via @nestjs/mongoose
  ...

Guards, pipes, interceptors, filters and middleware in a REST API
↑↓ navigate • ⏎ select
Enter fullscreen mode Exit fullscreen mode

That's all! You'll end up with a standalone project that has only that sample's files, plus the next steps to run it.

Why?

All those samples live inside the nestjs/nest monorepo, next to the framework's source code. So, in order to run one of them, we usually clone the whole repo (or play with git sparse-checkout), cd into sample/<some-sample> and only then install & run it. Also, you need to know which sample you are looking for beforehand.

Of course that you can use tools like degit to download a single folder, but you still have to browse GitHub to find out the sample's name first.

How?

try-nest does the following for you:

  1. Reads the list of samples from the nestjs/nest repo at run time. There's no hardcoded list, so new samples show up as soon as they land on master
  2. Reads the description field of each sample's package.json to annotate the list
  3. Downloads the repo tarball and writes only the sample/<name>/ folder into your directory. Thus the result has no reference back to the monorepo

Every prompt has a flag as well, so you can use it in scripts or CI:

## List all available samples
## (add --json for a machine-readable output)
npx try-nest@latest --list

## Skip all the prompts
npx try-nest@latest --sample 01-cats-app --dir my-cats-app --no-install
Enter fullscreen mode Exit fullscreen mode

which outputs:

Done. 01-cats-app is ready in my-cats-app.

Next steps:
  cd my-cats-app
  npm install
  npm run start:dev

Upstream: https://github.com/nestjs/nest/tree/master/sample/01-cats-app
Enter fullscreen mode Exit fullscreen mode

This tool is heavily inspired by try-prisma.

Check out the source code here: https://github.com/micalevisk/try-nest

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.