1

I have package.json file, that contains 10 dependencies,

Every time I do npm install -g,all the dependencies are downloaded again, and this takes a long time.

Is there a way to take the existing dependencies from the local node_modules directory ?

This is my package.json dependencies:

  "dependencies": {
    "body-parser": "~1.16.0",
    "colors": "^1.1.2",
    "console-stamp": "^0.2.5",
    "cookie-parser": "~1.4.3",
    "dateformat": "^2.0.0",
    "debug": "~2.6.0",
    "express": "~4.14.1",
    "ip": "^1.1.5",
    "jade": "~1.11.0",
    "lodash": "^4.17.4",
    "morgan": "~1.7.0",
    "nodemon": "^1.11.0",
    "promise": "^8.0.1",
    "rand-token": "^0.3.0",
    "random-date-generator": "^1.0.2",
    "restify": "^4.3.0",
    "restify-cookies": "^0.2.2",
    "serve-favicon": "~2.3.2"
  }

Thanks,

ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35
user2274204
  • 315
  • 3
  • 6
  • 18

2 Answers2

2

npm install -g installs the current package context (i.e, the current working directory) as a global package. You do not need this for installing the project dependencies. Instead you can use npm install which will install/update dependencies in node_modules folder. By default, npm install will install all modules listed as dependencies in package.json and they do not install all dependencies every time and will only update when a change has been made to package.json or the node_modules folder is cleared out.

use npm install -g option to install packages globally like cordova, gulp, yeoman etc where you need them to be available throughout multiple projects.

Vikram Palakurthi
  • 2,406
  • 1
  • 27
  • 30
1

Conceptually, the input to npm-install is a package.json, while its output is a fully-formed node_modules tree.

This shouldn't be an issue because npm uses the cache. It returns 304 if you have already installed the module, you can check it by applying --verbose with npm install command if given it's the same version of the dependency.

What I mean is:

$ npm install express

npm http fetch GET http://registry.npmjs.org/express

npm http fetch GET 200 http://registry.npmjs.org/express

at last, it shows:

++ express@4.16.2

added 49 packages in 12.547s

If you run it again then you will see

$ npm install express

npm http fetch GET http://registry.npmjs.org/express 408ms (from cache)

npm http fetch GET 304 http://registry.npmjs.org/express 408ms (from cache)

at last, it shows:

skipping write for package.json because there were no changes

++ express@4.16.2

updated 1 package in 4.11s

It hits the cache and won't download it again.

When running npm install for a second time on a project then it will not download it again only npm will check the first level of modules to ensure they're installed.

but not traverse the dependency tree to ensure all sub-module dependencies are installed. You can run npm outdated to check if modules are missing but npm won't install them for you.

npm update express updates the express module and its dependencies. So updates are when you already have the module and wish to get the new version. the update will always update to the latest version, regardless of package.json, while npm install will respect the version given in package.json.

Suhas Gavad
  • 1,199
  • 1
  • 8
  • 12