npm install -g再次下载所有依赖项

时间:2018-03-05 10:13:11

标签: node.js npm npm-install

我有package.json个文件,其中包含10个依赖项,

每次执行npm install -g时,都会再次下载所有依赖项,这需要很长时间。

有没有办法从本地node_modules目录中获取现有的依赖项?

这是我的package.json依赖项:

  "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"
  }

谢谢,

2 个答案:

答案 0 :(得分:1)

npm install -g将当前包上下文(即当前工作目录)安装为全局包。您不需要这个来安装项目依赖项。相反,您可以使用npm install来安装/更新node_modules文件夹中的依赖项。默认情况下,npm install将在package.json中安装列为依赖项的所有模块,并且它们不会每次都安装所有依赖项,只会在对package.json或{{进行更改时更新1}}文件夹被清除。

使用node_modules选项全局安装软件包,如cordova,gulp,yeoman等,您需要在多个项目中使用它们。

答案 1 :(得分:0)

从概念上讲,npm-install的输入是package.json,而其输出是完整格式的node_modules树。

这应该不是问题,因为npm使用缓存。如果您已经安装了模块,则返回304,如果给出--verbose npm install命令,则可以通过npm install检查它是否为相同版本的依赖项。

我的意思是:

$ npm install express

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

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

最后,它显示:

++ express@4.16.2

在12.547s中添加了49个包

如果再次运行,则会看到

$ npm install express

npm http fetch GET http://registry.npmjs.org/express 408ms(来自缓存)

npm http fetch GET 304 http://registry.npmjs.org/express 408ms(来自缓存)

最后,它显示:

为package.json跳过写,因为没有更改

++ express@4.16.2

在4.11s中更新了1个包

它会点击缓存,不会再次下载。

当在项目上第二次运行npm update express时,它将不会再次下载它只有npm将检查第一级模块以确保它们已安装。

但不遍历依赖关系树以确保安装所有子模块依赖关系。你可以运行npm过时来检查模块是否缺失但是npm不会为你安装它们。

package.json更新express模块​​及其依赖项。因此,当您已经拥有该模块并希​​望获得新版本时,更新即可。无论npm install如何,更新都将始终更新为最新版本,而package.json将尊重> [05-Mar-2018 09:42:01 America/Toronto] PHP Notice: Undefined > variable: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 > Transitional//EN" > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> > > <html xmlns="http://www.w3.org/1999/xhtml"> <head> > <title>Contest Entry Form</title> > <meta http-equiv="content-type" content="text/html;charset=utf-8" /> > <meta name="robots" content="noindex,nofollow"> > > <link rel="icon" href="https://wonderfest.com/wp-content/uploads/2017/01/favicon.ico" > type="image/x-icon"/> > <link href="../css/printout.css" type="text/css" rel="stylesheet"/> > <link href="https://fonts.googleapis.com/css?family=Libre+Barcode+39+Extended|Oxygen+Mono|Roboto" > rel="stylesheet"> > > <!--<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">--> > <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> > </head> <body> > <div id="container"> > <div class="no-print"> > <p><strong><u>Instructions</u></strong></p> > in /Users/rwaddell/Dropbox/htdocs/wonderfest/secure/contest/common/php/printout.php > on line 529 [05-Mar-2018 09:42:01 America/Toronto] PHP Fatal error: > Uncaught exception 'Mpdf\MpdfException' with message 'Error detected. > PDF file generation aborted: Undefined variable: <!DOCTYPE html > PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> > > <html xmlns="http://www.w3.org/1999/xhtml"> <head> > <title>Contest Entry Form</title> > <meta http-equiv="content-type" content="text/html;charset=utf-8" /> > <meta name="robots" content="noindex,nofollow"> > > <link rel="icon" href="https://wonderfest.com/wp-content/uploads/2017/01/favicon.ico" > type="image/x-icon"/> > <link href="../css/printout.css" type="text/css" rel="stylesheet"/> > <link href="https://fonts.googleapis.com/css?family=Libre+Barcode+39+Extended|Oxygen+Mono|Roboto" > rel="stylesheet"> > > <!--<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">--> > <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> > </head> <body> > <div id="container"> in /Users/rwaddell/Dropbox/htdocs/wonderfest/secure/contest/common/php/printout.php > on line 529 Marker - Mar 5, 2018, 10:06:39 AM 中提供的版本。

相关问题