Recover missing package.json with npm-collect
I recently took over a spare time project with multiple Git repos and branches where the package.json
had been removed from all branches without being able to restore them. The funny part was that all branches contained a node_modules
folder!
I did some digging and came across the Node package npm-collect that provides ways to create a new package.json
from node_modules
folders.
All we have to do is to install npm-collect globally:
npm install -g npm-collect
Run the following command from the folder where the package.json
is missing but the node_modules
folder exists:
npm-collect --new --save
Depending on the number of (nested) node folders, this might take some time, but it is worth it! You should end up with a new package.json
file in your project folder.
$ npm-collect --help
Usage: npm-collect <options>
Options:
--devDependencies process dev-dependencies instead of dependencies
--new include modules that are not found in package.json, but are installed
--save write changes to package.json
-v <number> verboselevel (0=silent,1=normal,2=debug)
npm-collect — https://www.npmjs.com/package/npm-collect
P.S. Moreover, npm-collect can be used to enrich an existing package.json
file with missing packages from the node_modules
folder. For instance, if you forgot to use —-save
, the package will be installed but not added to the package.json
. npm-collect will fix that for you.
Cheers \w