How to add Youtube Videos in Next.js ?
Last Updated :
26 Jul, 2024
In this article, we are going to learn how we can add Youtube Video in NextJs. NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac. The linking of dynamic paths helps in rendering your NextJS components conditionally.
Approach
To add our Youtube video we are going to use the react-youtube package. The react-youtube package helps us to add Youtube videos anywhere in our app. So first, we will install the react-youtube package and then we will add a youtube video on our homepage.
Steps to Create NextJS Application
You can create a new NextJs project using the below command:
npx create-next-app gfg
Install the required package: Now we will install the react-youtube package using the below command:
npm i react-youtube
Project Structure: It will look like this.

The updated dependencies in the package.json file are:
"dependencies": {
"next": "14.2.4",
"react": "^18",
"react-dom": "^18",
"react-youtube": "^10.1.0"
}Adding the Youtube Video
We can easily add the Youtube videos in our app after installing the react-youtube package. For this example, we are going to add a text highlighter to our homepage.
Add the below content in the index.js file:
JavaScript
// pages/index.js
import React from "react";
import YouTube from "react-youtube";
export default class YoutubeVideo extends React.Component {
render() {
const opts = {
height: "390",
width: "640",
playerVars: {
autoplay: 1,
},
};
return (
<div>
<h3>GeeksforGeeks - Youtube</h3>
<YouTube videoId="sTnm5jvjgjM"
opts={opts} onReady={this._onReady} />
</div>
);
}
_onReady(event) {
event.target.pauseVideo();
}
}
Explanation: In the above example first, we are importing the Youtube component from the installed package. After that, we are creating a new constant variable to store the settings of the video player. Then we are adding our videoId and options to our Youtube Component. You can find the videoId in the youtube video link.
Steps to run the application: Run the below command in the terminal to run the app.
npm run dev
Output:
Explore
Next js basics
Next js Routing
Next js Data Fetching
Next js Rendering
Next js Styling
Next js Optimizing
Next js Configuring
Next js Deploying
Next js Components
Next js File Conventions