Home  >  Q&A  >  body text

How to exclude a folder from building next js but still read from it?

<p>I have a folder with many json files. I want to exclude it from the build as it increases the build size. But I also need to get the data from these files and it can't be stored in the database. Is there any way to achieve this? </p> <p>I tried excluding the folder in next.config.js but couldn't find any changes</p>
P粉478445671P粉478445671391 days ago520

reply all(1)I'll reply

  • P粉322319601

    P粉3223196012023-08-29 20:01:50

    First, you need to exclude the folders in next.config.js, assuming all the data you need to exclude is in the data folder:

    const nextConfig = {
      // ...
      exclude: ['data'],
    };

    Secondly, you need to import the data folder using require in order to use json data:

    const users = require('./data/users.json');

    reply
    0
  • Cancelreply