0

Let's say, I have install some new packages, uninstall some packages, update some packages to new versions. All changes are saved into package.json. When I pull the changes with this new package.json, is there a single command to do the synchronisation between the locally installed packages and those specified in the updated package.json?

I am looking for something like:

$ npm syncrhonise
hllau
  • 9,879
  • 7
  • 30
  • 35

2 Answers2

0

npm install will install all packages specified in package.json. npm update will do the same but will also go a find any new versions of those packages. You can uninstall an individual package using npm uninstall <package>. There are already solutions to remove all packages here

Community
  • 1
  • 1
Alex Logan
  • 1,221
  • 3
  • 18
  • 27
  • If I was you I would just delete your node modules folder then run `npm update` – Alex Logan Apr 07 '16 at 08:56
  • Wouldn't it be wasting resources to download and fetch packages that are already installed again? – hllau Apr 07 '16 at 08:57
  • Depends how many packages you have. If you 2000 then maybe but otherwise who cares. Also npm caches quite a bit so you'd probably see a load of 304 codes if you ran the update – Alex Logan Apr 07 '16 at 09:03
  • Some of the libraries require compilations. This is a workaround, but the speed will be very slow if I have to run this every time. – hllau Apr 07 '16 at 09:16
0

I found two ways:

First npm prune will uninstall everything not listed in your package.json

npm prune [<name> [<name ...]]

This command removes "extraneous" packages. If a package name is provided, then only packages matching one of the supplied names are removed.

Extraneous packages are packages that are not listed on the parent package's dependencies list.

Documentation available at prune.

Second You could remove your node_modules/ folder and then reinstall the dependencies from package.json.

rm -rf node_modules/
npm install

This would erase all installed packages in the current folder and only install the dependencies from package.json. If the dependencies have been previously installed npm will try to use the cached version, avoiding downloading the dependency a second time.

Windows Trick

Due to its folder nesting Windows can’t delete the folder as its name is too long. To solve this, install RimRaf:

npm install rimraf -g

rimraf node_modules
Zeeshan Hassan Memon
  • 8,105
  • 4
  • 43
  • 57