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
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.
2 Comments
some-user
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.Christian Ulbrich
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. :)
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
Goutham
I need help how to use this env in our nuxt app.
Zachary Russell Heineman
@Goutham what kind of help?
balu
@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