question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pushing tag after npm version

See original GitHub issue

I’m trying to push the latest version created via npm version to github, but the following code doesn’t work

package.json

{
  script: {
    "version": "node pushVersion.js"
  }
}

pushVersion.js

const fs = require('fs')
const packageJson = fs.readFileSync('./package.json')
const version = JSON.parse(packageJson).version || 0

const simpleGit = require('simple-git')
const git = simpleGit()

let latestTag = `v${version}`

git
  .push(['origin', latestTag])
  .exec(() => console.log('%s is on github', latestTag))

If I inject .addTag(latestTag) before .push(...) - the tag gets pushed, but I recieve an error that the tag already exist.

Getting the tags with .tags(...) will provice me a list of tags which doesn’t contain the latest version bumped by npm version.

How can I push a single tag after npm version? manually typing `git push origin <tagname> works.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Ornhojcommented, Feb 22, 2022

That was perfect! that does exactly what I wanted to achieve. Only “issue” is the git.tags() which doesn’t return the TagList, hence tags.all fails since tagswill be undefined.

I’ve added the final setup (with corrections) here for future references:

projectRoot/package.json

{
  ...
  script: {
    "postversion": "node pushVersion.js"
    ...
  }
}

projectRoot/pushVersion.js

const simpleGit = require('simple-git')
const git = simpleGit()
const latest = `v${require('./package.json').version}`

Promise.resolve()
  .then(() => git.tags((err, tags) => tags)) // changed to return the tagList from git.tags()
  .then((tags) => {
    return tags.all.some((tag) => tag === latest)
  })
  .then((alreadyTagged) => {
    if (!alreadyTagged) return git.tag(latest)
  })
  .then(() => {
    git.push('origin') // Added to ensure everything committed is pushed as well
    return git.push('origin', latest)
  })
  .then(() => console.log('Tag "%s" is now available on Github', latest))
1reaction
steveukxcommented, Feb 21, 2022

Do you have NPM’s git-tag-version set to false when you run npm version? If not, there should be a git tag already created in your local.

Try switching to:

const git = simpleGit();
const latest = `v${ require('./package.json').version }`;

Promise.resolve()
  .then(git.tags())
  .then(tags => {
    console.log(`Tag List: `, tags);
    return tags.all.some(tag => tag === latest);
  })
  .then(alreadyTagged => {
    if (!alreadyTagged) return git.tag(latest);
  })
  .then(() => {
    return git.push('origin', latest);
  })
  .then(() => console.log('DONE'))
;

If that’s still not working as expected - please can you run your code with debug enabled and include the relevant bit of the log in the issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to automatically push tag to git after npm version ...
npm version : Run this in a package directory to bump the version and write the new data back to package. json [..]...
Read more >
Add git version tag after publishing to npm · Muffin Man
It is supported by npm, and it will be executed after every npm publish . Only thing left to do is to add...
Read more >
Adding dist-tags to packages - npm Docs
By default, running npm publish will tag your package with the latest dist-tag. To use another dist-tag, use the --tag flag when publishing....
Read more >
Understanding NPM Versioning With Git Tags | by Davis Barber
At first glance, this command looks simple. npm version bumps our package version for us, altering the version in our package.json file in...
Read more >
How to use npm version lifecycle scripts to make ... - Github-Gist
you bump the version of your package and publish to npm; the new version rolls out to users. However, you never make a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found