11

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 [email protected] -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

3 Answers 3

32

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.

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

4 Comments

I meant to come back and answer this question myself. After I posted this I went and researched more and found this answer: 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!
Just for the sake of clarification, with [ID] you mean hash, right?
@dawn [ID] = commit id.
@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)
2

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.

Comments

-1

git checkout -- package-lock.json

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.