Home  >  Q&A  >  body text

How to read NextJS's build ID?

Using NextJS, you can create a build ID according to the documentation: https://nextjs.org/docs/app/api-reference/next-config-js/generateBuildId

It can also be obtained according to the latest git version: https://www.npmjs.com/package/next-build-id

But the question is: how do I read this build ID? I want to display it inside the window console.

P粉904450959P粉904450959259 days ago509

reply all(1)I'll reply

  • P粉186897465

    P粉1868974652024-02-04 10:21:56

    I haven't tested it, but I think you could try the old way of passing the env variable via the env key in the configuration (I guess it's deprecated now, but it should still work) like this:

    const nextBuildId = require('next-build-id')
    
    const buildId = nextBuildId({ dir: __dirname })
    
    const nextConfig = {
      generateBuildId: () => buildId 
      env: {
        BUILD_ID_ENV: buildId
      },
    };

    Will be provided later as process.env.BUILD_ID_ENV

    To use it in a client component you may have to name it NEXT_PUBLIC_BUILD_ID_ENV, not sure.

    reply
    0
  • Cancelreply