skip to Main Content

With NextJS, it’s possible to create a build ID, according to the docs : https://nextjs.org/docs/app/api-reference/next-config-js/generateBuildId

It’s also possible to get it according to the last git version : https://www.npmjs.com/package/next-build-id

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

2

Answers


  1. I have not tested it, but I think you can try the old (I guess it’s deprecated now, but it still should work) way of passing env vars through the env key in the config, like that:

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

    And it will be available later as process.env.BUILD_ID_ENV

    To use it in the client side component potentially you would have to name it as NEXT_PUBLIC_BUILD_ID_ENV, not sure about it.

    Login or Signup to reply.
  2. this works in server component because that package uses fs

    import buildId from "next-build-id";
    
    console.log("next-build-id", await buildId());
    

    proof of work:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search