Next.js Tutorial
Selected Reading
- Next.js - Discussion
- Next.js - Useful Resources
- Next.js - Quick Guide
- Next.js - CLI
- Next.js - Deployment
- Next.js - Environment Variables
- Next.js - Typescript
- Next.js - Response Helpers
- Next.js - API MiddleWares
- Next.js - API Routes
- Next.js - Shallow Routing
- Next.js - Imperitive Routing
- Next.js - Dynanic API Routes
- Next.js - Routing
- Next.js - Pre-Rendering
- Next.js - Global CSS Support
- Next.js - CSS Support
- Next.js - Meta Data
- Next.js - Static File Serving
- Next.js - Pages
- Next.js - Environment Setup
- Next.js - Overview
- Next.js - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Next.js - Environment Setup
Next.js - Environment Setup
As Next.js is a react based framework, we are using Node environment. Now make sure you have Node.js and npm installed on your system. You can use the following command to install Next.js −
npm install next react react-dom
You can observe the following output once Next.js is successfully installed −
+ react@16.13.1 + react-dom@16.13.1 + next@9.4.4 added 831 packages from 323 contributors and audited 834 packages in 172.989s
Now, let s create a node package.json −
npm init
Select default values while creating a package.json −
This utipty will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. package name: (nextjs) version: (1.0.0) description: entry point: (index.js) test command: git repository: keywords: author: pcense: (ISC) About to write to Node extjspackage.json: { "name": "nextjs", "version": "1.0.0", "description": "", "main": "index.js", "dependencies": { "next": "^9.4.4", "react": "^16.13.1", "react-dom": "^16.13.1" }, "devDependencies": {}, "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "author": "", "pcense": "ISC" } Is this OK? (yes)
Now update the scripts section of package.json to include Next.js commands.
{ "name": "nextjs", "version": "1.0.0", "description": "", "main": "index.js", "dependencies": { "next": "^9.4.4", "react": "^16.13.1", "react-dom": "^16.13.1" }, "devDependencies": {}, "scripts": { "test": "echo "Error: no test specified" && exit 1", "dev": "next", "build": "next build", "start": "next start" }, "author": "", "pcense": "ISC" }
Create pages directory.
Create a pages folder within nextjs folder and create an index.js file with following contents.
function HomePage() { return <span>Welcome to Next.js!</span> } export default HomePage
Start Next.js Server
Run the following command to start the server −.
npm run dev > nextjs@1.0.0 dev Node extjs > next ready - started server on http://localhost:3000 event - compiled successfully event - build page: / wait - compipng... event - compiled successfully event - build page: /next/dist/pages/_error wait - compipng... event - compiled successfully
Verify Output
Open localhost:3000 in a browser and you will see the following output.
