2

I am going through a MEAN stack development PDF .

Every Time i need to create a new API , they ask to add required packages in packages.json file and run npm install.

Example :

 {
  "name": "node-api",
   "main": "server.js",
   "dependencies": {
   "morgan": "~1.5.0",
   "express": "~4.10.3",
   "body-parser": "~1.9.3",
   "mongoose": "~3.8.19",
    "bcrypt-nodejs": "0.0.3"
  }
 }

then do in terminal:

npm install

So if I create new API say API2 then again i need to do the same and all the packages are downloaded again for node_modules:

My Question is: can't we download and store these packages locally and use whenever we require them, just like we do in Python or C++, instead of Downloading every time again.

nbro
  • 15,395
  • 32
  • 113
  • 196
smartsn123
  • 115
  • 9
  • You can simply copy the `node_modules` folder to your disk storage, after the first `npm install` and every subsequent `npm install` (when you update versions or dependencies). – psiyumm Nov 14 '15 at 13:35
  • and then if i add new packages and do npm install , will it download packages selectively that at not available ? – smartsn123 Nov 14 '15 at 13:37

2 Answers2

1

you can install package globaly with the g flag. E.g : npm install -g <package_name>.
if you want to install a package globaly with a specific version : npm install -g <package_name>@<version>.

But you have to understand that each applications need a specific version of a package that why it's very useful to have the ability to install the right pacakge with a version compatible to run it. In python, we use for example virtualenv.

For example, an application A can maybe have 3 packages in common that application B have but, the version of these packages are not the same because packages version of A applications are not comptatible to run the B application. That why, you have to install all packages of each apps with npm install.

Louis Barranqueiro
  • 10,058
  • 6
  • 42
  • 52
0

Each application has a "packages.json" file that list all dependencis so i think smartsn123 is right it's better to have a global repository instead of creating a local one for each app and use "packages.json" to select from global repo. it gets worse when you notice that some dependencies have a node_modules folder in them.

david
  • 465
  • 5
  • 11