Hello folks! welcome back to a new section of our tutorial on Next.js. In this tutorial, we will be discussing about Next.js TypeScript Support.
Next.js has excellent support for typescript. Following below are few steps to enabling typescript in your project.
Next.js has excellent support for typescript. Following below are few steps to enabling typescript in your project.
Create tsconfig.json
Create an tsconfig.json in root directory. We are keeping it empty initially. Now start the server.
Next.js will detect tsconfig.json and display the following message on console.
Next.js will detect tsconfig.json and display the following message on console.
npm run dev > nextjs@1.0.0 dev D:\Node\nextjs > next ready - started server on http://localhost:3000 It looks like you're trying to use TypeScript but do not have the required package(s) installed. Please install typescript, @types/react, and @types/node by running: npm install --save-dev typescript @types/react @types/node If you are not trying to use TypeScript, please remove the tsconfig.json file from your package root (and any TypeScript files). ...
Install Typescript
Now run the npm install command to install typescript and related libraries.
npm install --save-dev typescript @types/react @types/node ... + @types/node@14.0.23 + @types/react@16.9.43 + typescript@3.9.6 added 5 packages from 72 contributors and audited 839 packages in 27.538s ...
Start Next.js Server
Run the following command below to start the server -
npm run dev > nextjs@1.0.0 dev D:\Node\nextjs > next ready - started server on http://localhost:3000 We detected TypeScript in your project and created a tsconfig.json file for you. Your tsconfig.json has been populated with default values. event - compiled successfully wait - compiling... event - compiled successfully
Open tsconfig.json
Next.js server has modified tsconfig.json.
{ "compilerOptions": { "target": "es5", "lib": [ "dom", "dom.iterable", "esnext" ], "allowJs": true, "skipLibCheck": true, "strict": false, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve" }, "exclude": [ "node_modules" ], "include": [ "next-env.d.ts", "**/*.ts", "**/*.tsx" ] }
Create hello.ts
Create hello.ts in pages/api directory which will act as a rest service for us.
import { NextApiRequest, NextApiResponse } from 'next' export default (_: NextApiRequest, res: NextApiResponse) => { res.status(200).json({ text: 'Welcome to WebDesignTutorialz' }) }
Start Next.js Server
Run the following command below to start the server -
npm run dev > nextjs@1.0.0 dev \Node\nextjs > next ready - started server on http://localhost:3000 event - compiled successfully event - build page: / wait - compiling... event - compiled successfully event - build page: /next/dist/pages/_error wait - compiling... event - compiled successfully
Output
Open http://localhost:3000/api/hello in any browser of your choice to see the following result -
{"text":"Welcome to WebDesignTutorialz"}
Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we are going to be studying about Next.js Environment Variables.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.