7

I'm working on a git pull request for Mocha.

I'm running into an error related to my package-lock.json file in where I accidentally updated the package ansi-regex from 2.1.1 to 3.0.0

I'm now having issues getting this version back to it's original, which was requested by the repo owners.

The major issue I am encountering is that this npm package only exists within package-lock

 "ansi-regex": {
      "version": "3.0.0",
      "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz",
      "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg="
    },

it doesn't exist inside the main package.json file.

Through research, I've found that it's not suggested to delete the package-lock file and regenerate it with npm i because that could introduce even more changes.

I've also tried the command:

git checkout --theirs package-lock.json
git add package-lock.json 

But the version remains 3.0.0

I've also tried to run a

npm uninstall --save ansi-regex@3.0.0 -package-lock.json

To manually remove the file, but the command completes without removing any packages.

I'm not sure how to get this version back to the one setup by the repo owners and could really use some help figuring out next steps.

Thank you

Dan
  • 361
  • 1
  • 5
  • 17

2 Answers2

26

You need to find the ID of a commit before the one where you modified package-lock.json, and restore the content of package-lock.json from that commit.


Using the command line (open git-bash for example) :

  • You can view the list of commits that modified package-lock.json using :

    git log package-lock.json
    
  • You can set package-lock.json back to its version in commit [ID] using :

    git checkout [ID] -- package-lock.json
    

You can do the same actions from a graphical client, just be sure to select the file from the past commit and checkout only that file, rather than checkout the whole commit.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • I meant to come back and answer this question myself. After I posted this I went and researched more and found this answer: https://stackoverflow.com/questions/27954247/how-to-undo-npm-update Where they said something similar to what you suggested, so I'll make this as correct incase someone comes searching for this in the future Thank you for the detailed response! – Dan Jan 08 '20 at 23:23
  • 1
    Just for the sake of clarification, with [ID] you mean hash, right? – dawn Jun 02 '22 at 17:34
  • @dawn [ID] = commit id. – LastM4N Jun 03 '22 at 11:10
  • @dawn: yes, the `sha` of target commit (note: if you have a tag or a branch or any name that points to this commit, you can also use that tag/branch/name) – LeGEC Jun 03 '22 at 11:12
1

Easier way is to just open the source project and switch to the branch with which you will be comparing your branch. (Say 'main'). Then, just copy the whole content of package-lock.json and paste it in your branch.