Environment Setup in NextJS
🌍 Environment Setup in Next.js
Setting up a Next.js development environment involves installing dependencies, configuring environment variables, and optimizing settings for development and production.
📌 1. Install Next.js
To start a new Next.js project, use:
npx create-next-app my-next-app
or using Yarn:
yarn create next-app my-next-app
Then, navigate to your project:
cd my-next-appnpm run dev
✅ Runs the app on http://localhost:3000
📌 2. Project Structure
After installation, your project looks like this:
my-next-app/│-- pages/ # Page components (Auto-Routing)│-- public/ # Static assets (images, favicons)│-- styles/ # Global & component styles│-- components/ # Reusable UI components│-- next.config.js # Next.js configuration│-- .env.local # Environment variables│-- package.json # Project dependencies
✅ Pages inside pages/ are automatically turned into routes.
📌 3. Running Next.js Locally
To start the development server:
npm run dev
✅ Hot reloads on code changes
✅ Runs on http://localhost:3000
For production build, use:
npm run build && npm start
✅ Optimizes for production
📌 4. Environment Variables
Use .env.local for environment-specific settings.
🔹 Example: .env.local
NEXT_PUBLIC_API_URL=https://api.example.comSECRET_KEY=123456
🔹 Using Variables in Code
const apiUrl = process.env.NEXT_PUBLIC_API_URL;console.log(apiUrl);
✅ Prefix with NEXT_PUBLIC_ for client-side usage
✅ No need to restart server after changes
📌 5. Setting Up TypeScript (Optional)
To add TypeScript, run:
npm install --save-dev typescript @types/react @types/node
Then rename any .js file to .tsx, and Next.js auto-generates tsconfig.json.
✅ Type safety for better development
📌 6. Adding ESLint & Prettier
To maintain clean code:
npm install --save-dev eslint prettier eslint-config-next
Then create .eslintrc.json:
: ["next/core-web-vitals"]}
Run ESLint:
✅ Ensures best practices
📌 7. Setting Up Custom Next.js Configuration
Modify next.config.js to customize settings.
🔹 Example: Enabling Image Optimization
module.exports = { images: { domains: ["example.com"], },};
✅ Optimizes external images
📌 8. Debugging in Next.js
Enable debugging in VS Code:
Create
.vscode/launch.json
{ "version": "0.2.0", "configurations": [ { "name": "Next.js Debugging", "type": "node", "request": "attach", "port": 9229, "sourceMaps": true, "protocol": "inspector" } ]}
Start the app with:
NODE_OPTIONS='--inspect' next dev
✅ Enables debugging inside VS Code
📌 9. Using Docker for Next.js
Create a Dockerfile:
FROM node:18-alpineWORKDIR /appCOPY package.json yarn.lock ./RUN yarn installCOPY . .RUN yarn buildCMD ["yarn", "start"]
Build & run the container:
docker build -t my-next-app .docker run -p 3000:3000 my-next-app
✅ Useful for deploying in containers
📌 10. Summary: Key Environment Setup Steps
| Step | Command |
|---|---|
| Install Next.js | npx create-next-app my-app |
| Start Dev Server | npm run dev |
| Create Environment Variables | .env.local |
| Add TypeScript | npm install --save-dev typescript |
| Add ESLint | npm install --save-dev eslint prettier |
| Debugging | NODE_OPTIONS='--inspect' next dev |
| Run in Docker | docker build -t my-next-app . |