Open In App

How to find unused files in Next.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn How we can find unused files in our NextJS project. 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 find the unused file first we will create 2 new javascript files in the root of our project. After that, we will import one file into our index.js file then we will install and run the next-unused module to check the unused file,

Create NextJS Application:

Step 1: You can create a new NextJs project using the below command:

npx create-next-app gfg

Project Structure: It will look like this.

Unused Files: You can easily find unused files in NextJs using the next-unused module. For this first, we will create one new folder named ‘component’ inside the root of the project and inside this folder, we will create two new files named ‘unused1’ and ‘used1’. Then we will add the below content in our files.

Step 2: Creating a new file named unused.js in the root of our project and inside that file create a new function called Unused().




import React from 'react'
  
export default function Unused() {
    return (
        <div>
            Unused File
        </div>
    )
}


Step 3: Creating a new file named used.js in the root of our project and inside that file create a new function called Used().




import React from 'react'
  
export default function Used() {
    return (
        <div>
            Used File
        </div>
    )
}


Step 4: In this step, we will import our Used.js file into our Index.js file. Then we will create a function with the name Home and inside that, we will call our Used component.




import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import Used from '../component/used1'
  
export default function Home() {
  return (
    <div className={styles.container}>
      <Used/>
    </div>
  )
}


Here we are importing our used1.js file. 

Step 5: Now to find unused files in our project we are going to install one module name ‘next-unused’ using the below command:-

npm i next-unused

Step 6: Now we will add the script and property for our next-unused module in our package.json file.

Add Script:

"scripts": {
  "find:unused": "next-unused"
}

Add Property:

{
 "next-unused": {
   "alias": {},
   "include": ["component"],
   "exclude": [],
   "entrypoints": []
 }
}

Step to run the application: Now you can run the module using the below command in the terminal command in the terminal.

npm run find:unused

Output:



Last Updated : 04 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads