2

I got several issues after I deleted package-lock.json and node_modules, and figured out that I installed a lot of packages without --save or --save-dev.

I am now wondering how to fix this. Some of my packages are not even needed in my project.

I need to identify which packages are useful, so i used depcheck https://www.npmjs.com/package/depcheck which doesn't exactly work as I would like it to because some "unused devDependencies" are actually used, and I don't think the missing dependencies are here...

 depcheck .                    
unused dependencies             
 depcheck                      
 instafeed.js                  
 leaflet                       
 leaflet.icon.glyph            
 v-autocomplete                
 vue2-leaflet                  
unused devDependencies          
 autoprefixer                  
 browser-jsonp                 
 css-mqpacker                  
 cssnano                       
 gulp                          
 gulp-postcss                  
 gulp-sass                     
 laue                          
 lodash.orderby                
 postcss-assets                
 resolve-url-loader            
 sass                          
 sass-loader                   
 v-datatable-light             
 vee-validate                  
 vue-bootstrap-datetimepicker  
 vue-country-region-select     
 vue-instagram                 
 vue-resource                  
 vue-slider-component          
 vue-stack-grid                
 vue-star-rating               
 vue-tel-input                 
 vuejs-datepicker              
missing dependencies            
 laravel-elixir                
 react                         
 react-dom 

For example I got a loat of errors refering to postcss-loader, css-loader, style-loader etc and these don't appear at all here. And my project is in vuejs so i don't get what react is doing here...

So i would like to identify the missing packages (from package-lock.json maybe?) with their actual version, and save them in package.json.

Is this posisble?

Thanks a lot, these would save my life :D

EDIT: I saw this post How to npm install --save all packages already installed? and something like this npm install ls node_modules --save would be a good start, but I would like to add a version (doesn't work anymore)

[EDIT] Thanks for your answers, I added this in a test.js file and executed it (node test.js)

const fs = require('fs');
const packageLock = require('./package-lock.json');
const package = require('./package.json');

package.dependencies = Object.entries(packageLock.dependencies)
.reduce((deps, [dep, { version }]) => Object.assign(deps, { [dep]: version }), {});

fs.writeFileSync('./package-new.json', JSON.stringify(package, null, 2));

Now I have a file containing all dependencies and can just add the necessary ones to my package.json file.

tony19
  • 125,647
  • 18
  • 229
  • 307
Maxiss
  • 986
  • 1
  • 16
  • 35
  • 1
    Possible duplicate of [Is there a way to extract package.json from package-lock.json?](https://stackoverflow.com/questions/54167989/is-there-a-way-to-extract-package-json-from-package-lock-json) – Estus Flask Jun 06 '19 at 08:22
  • 1
    This may be not a good choice because original version constraints are lost and this will result in making the project depend on nested dependencies. Considering you forgot --save for several packages, identifying missing packages one by one and saving them may be a better way. – Estus Flask Jun 06 '19 at 08:26

1 Answers1

0

How about something like npm install --save $(ls node_modules)

Nate Bunney
  • 2,418
  • 2
  • 22
  • 31