2

I have a Nuxt/Vue app that I am deploying through Netlify. I'm trying to inject the git version number and hash into the body of the page. I'm assuming there is a pretty straightforward way to do this (but I don't know what it is!).

2 Answers 2

10

In package.json, you can pass the commit hash before call Nuxt:

"scripts": {
  "dev": "NUXT_ENV_CURRENT_GIT_SHA=`git rev-parse --short HEAD` nuxt",
  ...
},

Then, you can recover the value anywhere with process.env.NUXT_ENV_CURRENT_GIT_SHA.

Sign up to request clarification or add additional context in comments.

2 Comments

Does not work for me (Git Bash on Windows): 'NUXT_ENV_CURRENT_GIT_SHA' is not recognized as an internal or external command, operable program or batch file.
I suppose the problem is, that you need to explicitly set the shell to bash for npm on Windows, it does not suffice, calling it from git-bash. Albeit I have not tested it, kind of allergic to Windows. :)
1

In nuxt.config.js I'm setting an env (COMMIT_REF is an always-defined env in Netlify) but it is the long hash and I would be grateful if anyone had a solution for the short hash.

env: {
    COMMIT_REF: process.env.COMMIT_REF || 'COMMIT_REF'
}

3 Comments

I need help how to use this env in our nuxt app.
@Goutham what kind of help?
@Goutham You can access env in your app since Nuxt tells Webpack to inject the env variables into your application, see nuxtjs.org/docs/configuration-glossary/configuration-env

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.