Hatching the Nest β Installing NestJS Like a Pro (or a Chicken π) β
So you've met NestJS β the TypeScript-powered, Angular-inspired server-side superhero. Now it's time to install it, feed it some code, and watch it fly like... well, like a chicken with superpowers.
π οΈ Step 1: Prerequisites β Tools of the Trade β
Before you jump into the nest, make sure your toolbox is ready:
- π§ Node.js (v20+): The wise old wizard who runs your code
Install it from: https://nodejs.org
- π§ npm or yarn: The spellbooks (aka package managers)
You can check them like a boss:
node -v
npm -v
If you see versions and not weird errors, congrats! You're ready.
π£ Step 2: Global Nest CLI β The Nest Egg β
You only need to hatch this once:
npm i -g @nestjs/cli
π―οΈ This gives you the magical nest
command, which is basically the Harry Potter wand for backend devs.
π§ͺ Step 3: Create Your Nest β A New Project β
Run this spell:
nest new awesome-nest-app
And Nest will ask you to:
- Pick a package manager (npm or yarn β pick your poison)
- Then it installs everything like a wizard quietly brewing a potion.
Youβll get:
awesome-nest-app/
βββ src/
β βββ app.controller.spec.ts
β βββ app.controller.ts
β βββ app.module.ts
β βββ app.service.ts
βββ main.ts
βββ package.json
βββ tsconfig.json
Here's a brief overview of those core files:
File Name | Description |
---|---|
app.controller.ts | A basic controller with a single route. |
app.controller.spec.ts | The unit tests for the controller. |
app.module.ts | The root module of the application. |
app.service.ts | A basic service with a single method. |
main.ts | The entry file that uses NestFactory to create a Nest application instance. |
π Youβre now the proud parent of a brand-new NestJS app. Cute, huh?
π Step 4: Run Like the Wind β
Get inside your nest:
cd awesome-nest-app
And run it:
npm run start
To watch for changes in your files, you can run the following command to start the application:
npm run start:dev
Boom π₯! Your server is alive at:
http://localhost:3000
Open your browser and get a warm welcome:
Hello World!
π₯ Bonus: Add Swagger, Because Youβre Fancy β
Install Swagger to make your APIs strut like supermodels:
npm install --save @nestjs/swagger swagger-ui-express
Add this in main.ts
to unleash the docs:
import { SwaggerModule, DocumentBuilder } from "@nestjs/swagger";
const config = new DocumentBuilder()
.setTitle("My Nest API")
.setDescription("Swagger is here to flex your endpoints πͺ")
.setVersion("1.0")
.addBearerAuth()
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup("api", app, document);
Now visit:
http://localhost:3000/api
and boom, you got API docs β¨.
π₯ Conclusion β
Congratulations! You've just installed and launched your first NestJS app. Youβre now officially a Nestling Developerβ’, ready to build mighty server-side apps.
Stay tuned for the next chapter: "π¦ Modules β Like IKEA, But with Code."
Happy hacking, and may your backend never return a 500!