Skip to content
This repository
Browse code

Add save-dev and save-optional options to npm install

  • Loading branch information...
commit 21266a13bd3f1b8320ea51d2745e71f3a2cf264b 1 parent 66dae87
Tim Oxley authored April 07, 2012 isaacs committed April 17, 2012
20  doc/cli/config.md
Source Rendered
@@ -115,6 +115,8 @@ The following shorthands are parsed on the command-line:
115 115
 * `-l`: `--long`
116 116
 * `-desc`: `--description`
117 117
 * `-S`: `--save`
  118
+* `-D`: `--save-dev`
  119
+* `-O`: `--save-optional`
118 120
 * `-y`: `--yes`
119 121
 * `-n`: `--yes false`
120 122
 * `ll` and `la` commands: `ls --long`
@@ -584,6 +586,24 @@ Save installed packages to a package.json file as dependencies.
584 586
 
585 587
 Only works if there is already a package.json file present.
586 588
 
  589
+### save-dev
  590
+
  591
+* Default: false
  592
+* Type: Boolean
  593
+
  594
+Save installed packages to a package.json file as devDependencies.
  595
+
  596
+Only works if there is already a package.json file present.
  597
+
  598
+### save-optional
  599
+
  600
+* Default: false
  601
+* Type: Boolean
  602
+
  603
+Save installed packages to a package.json file as optionalDependencies.
  604
+
  605
+Only works if there is already a package.json file present.
  606
+
587 607
 ### searchopts
588 608
 
589 609
 * Default: ""
125  doc/cli/install.md
Source Rendered
@@ -7,10 +7,11 @@ npm-install(1) -- Install a package
7 7
     npm install <tarball file>
8 8
     npm install <tarball url>
9 9
     npm install <folder>
10  
-    npm install <name>
  10
+    npm install <name> [--save|--save-dev|--save-optional]
11 11
     npm install <name>@<tag>
12 12
     npm install <name>@<version>
13 13
     npm install <name>@<version range>
  14
+    npm install <name>@<version range>
14 15
 
15 16
 ## DESCRIPTION
16 17
 
@@ -35,88 +36,116 @@ after packing it up into a tarball (b).
35 36
 
36 37
 
37 38
 * `npm install` (in package directory, no arguments):
38  
-  Install the dependencies in the local node_modules folder.
39 39
 
40  
-  In global mode (ie, with `-g` or `--global` appended to the command),
41  
-  it installs the current package context (ie, the current working
42  
-  directory) as a global package.
  40
+    Install the dependencies in the local node_modules folder.
  41
+
  42
+    In global mode (ie, with `-g` or `--global` appended to the command),
  43
+    it installs the current package context (ie, the current working
  44
+    directory) as a global package.
  45
+
43 46
 
44 47
 * `npm install <folder>`:
45  
-  Install a package that is sitting in a folder on the filesystem.
  48
+
  49
+    Install a package that is sitting in a folder on the filesystem.
46 50
 
47 51
 * `npm install <tarball file>`:
48  
-  Install a package that is sitting on the filesystem.  Note: if you just want
49  
-  to link a dev directory into your npm root, you can do this more easily by
50  
-  using `npm link`.
51 52
 
52  
-  Example:
  53
+    Install a package that is sitting on the filesystem.  Note: if you just want
  54
+    to link a dev directory into your npm root, you can do this more easily by
  55
+    using `npm link`.
  56
+
  57
+    Example:
53 58
 
54  
-        npm install ./package.tgz
  59
+          npm install ./package.tgz
55 60
 
56 61
 * `npm install <tarball url>`:
57  
-  Fetch the tarball url, and then install it.  In order to distinguish between
58  
-  this and other options, the argument must start with "http://" or "https://"
59 62
 
60  
-  Example:
  63
+    Fetch the tarball url, and then install it.  In order to distinguish between
  64
+    this and other options, the argument must start with "http://" or "https://"
  65
+
  66
+    Example:
  67
+
  68
+          npm install https://github.com/indexzero/forever/tarball/v0.5.6
  69
+
  70
+* `npm install <name> [--save|--save-dev|--save-optional]`:
  71
+
  72
+    Do a `<name>@<tag>` install, where `<tag>` is the "tag" config. (See
  73
+    `npm-config(1)`.)
61 74
 
62  
-        npm install https://github.com/indexzero/forever/tarball/v0.5.6
  75
+    In most cases, this will install the latest version
  76
+    of the module published on npm.
63 77
 
64  
-* `npm install <name>`:
65  
-  Do a `<name>@<tag>` install, where `<tag>` is the "tag" config. (See
66  
-  `npm-config(1)`)
  78
+    Example:
67 79
 
68  
-  Example:
  80
+          npm install sax
69 81
 
70  
-        npm install sax
  82
+    `npm install` takes 3 exclusive, optional flags which save or update
  83
+    the package version in your main package.json:
71 84
 
72  
-  **Note**: If there is a file or folder named `<name>` in the current
73  
-  working directory, then it will try to install that, and only try to
74  
-  fetch the package by name if it is not valid.
  85
+    * `--save`: Package will appear in your `dependencies`.
  86
+
  87
+    * `--save-dev`: Package will appear in your `devDependencies`.
  88
+
  89
+    * `--save-optional`: Package will appear in your `optionalDependencies`.
  90
+
  91
+    Examples:
  92
+
  93
+          npm install sax --save
  94
+          npm install node-tap --save-dev
  95
+          npm install dtrace-provider --save-optional
  96
+
  97
+
  98
+    **Note**: If there is a file or folder named `<name>` in the current
  99
+    working directory, then it will try to install that, and only try to
  100
+    fetch the package by name if it is not valid.
75 101
 
76 102
 * `npm install <name>@<tag>`:
77  
-  Install the version of the package that is referenced by the specified tag.
78  
-  If the tag does not exist in the registry data for that package, then this
79  
-  will fail.
80 103
 
81  
-  Example:
  104
+    Install the version of the package that is referenced by the specified tag.
  105
+    If the tag does not exist in the registry data for that package, then this
  106
+    will fail.
  107
+
  108
+    Example:
82 109
 
83  
-        npm install sax@latest
  110
+          npm install sax@latest
84 111
 
85 112
 * `npm install <name>@<version>`:
86  
-  Install the specified version of the package.  This will fail if the version
87  
-  has not been published to the registry.
88 113
 
89  
-  Example:
  114
+    Install the specified version of the package.  This will fail if the version
  115
+    has not been published to the registry.
90 116
 
91  
-        npm install sax@0.1.1
  117
+    Example:
  118
+
  119
+          npm install sax@0.1.1
92 120
 
93 121
 * `npm install <name>@<version range>`:
94  
-  Install a version of the package matching the specified version range.  This
95  
-  will follow the same rules for resolving dependencies described in `npm-json(1)`.
96 122
 
97  
-  Note that most version ranges must be put in quotes so that your shell will
98  
-  treat it as a single argument.
  123
+    Install a version of the package matching the specified version range.  This
  124
+    will follow the same rules for resolving dependencies described in `npm-json(1)`.
  125
+
  126
+    Note that most version ranges must be put in quotes so that your shell will
  127
+    treat it as a single argument.
99 128
 
100  
-  Example:
  129
+    Example:
101 130
 
102  
-        npm install sax@">=0.1.0 <0.2.0"
  131
+          npm install sax@">=0.1.0 <0.2.0"
103 132
 
104 133
 * `npm install <git remote url>`:
105 134
 
106  
-  Install a package by cloning a git remote url.  The format of the git
107  
-  url is:
  135
+    Install a package by cloning a git remote url.  The format of the git
  136
+    url is:
108 137
 
109  
-        <protocol>://[<user>@]<hostname><separator><path>[#<commit-ish>]
  138
+          <protocol>://[<user>@]<hostname><separator><path>[#<commit-ish>]
110 139
 
111  
-  `<protocol>` is one of `git`, `git+ssh`, `git+http`, or
112  
-  `git+https`.  If no `<commit-ish>` is specified, then `master` is
113  
-  used.
  140
+    `<protocol>` is one of `git`, `git+ssh`, `git+http`, or
  141
+    `git+https`.  If no `<commit-ish>` is specified, then `master` is
  142
+    used.
114 143
 
115  
-  Examples:
  144
+    Examples:
116 145
 
117  
-        git+ssh://git@github.com:isaacs/npm.git#v1.0.27
118  
-        git+https://isaacs@github.com/isaacs/npm.git
119  
-        git://github.com/isaacs/npm.git#v1.0.27
  146
+          git+ssh://git@github.com:isaacs/npm.git#v1.0.27
  147
+          git+https://isaacs@github.com/isaacs/npm.git
  148
+          git://github.com/isaacs/npm.git#v1.0.27
120 149
 
121 150
 You may combine multiple arguments, and even multiple types of arguments.
122 151
 For example:
10  lib/install.js
@@ -225,7 +225,10 @@ function readDependencies (context, where, opts, cb) {
225 225
 // as dependencies to a package.json file.
226 226
 // This is experimental.
227 227
 function save (where, installed, tree, pretty, cb) {
228  
-  if (!npm.config.get("save") || npm.config.get("global")) {
  228
+  if (!npm.config.get("save") &&
  229
+      !npm.config.get("save-dev") &&
  230
+      !npm.config.get("save-optional") ||
  231
+      npm.config.get("global")) {
229 232
     return cb(null, installed, tree, pretty)
230 233
   }
231 234
 
@@ -259,8 +262,9 @@ function save (where, installed, tree, pretty, cb) {
259 262
     }
260 263
     if (er) return cb(null, installed, tree, pretty)
261 264
 
262  
-    var deps = npm.config.get("dev") ? "devDependencies" : "dependencies"
263  
-    deps = data[deps] = data[deps] || {}
  265
+    var deps = npm.config.get("save-optional") ? "optionalDependencies"
  266
+             : npm.config.get("save-dev") ? "devDependencies"
  267
+             : "dependencies"
264 268
 
265 269
     Object.keys(things).forEach(function (t) {
266 270
       deps[t] = things[t]
6  lib/utils/config-defs.js
@@ -189,6 +189,8 @@ Object.defineProperty(exports, "defaults", {get: function () {
189 189
     , registry : "http" + (httpsOk ? "s" : "") + "://registry.npmjs.org/"
190 190
     , rollback : true
191 191
     , save : false
  192
+    , "save-dev" : false
  193
+    , "save-optional" : false
192 194
     , searchopts: ""
193 195
     , searchexclude: null
194 196
     , searchsort: "name"
@@ -269,6 +271,8 @@ exports.types =
269 271
   , registry : [null, url]
270 272
   , rollback : Boolean
271 273
   , save : Boolean
  274
+  , "save-dev" : Boolean
  275
+  , "save-optional" : Boolean
272 276
   , searchopts : String
273 277
   , searchexclude: [null, String]
274 278
   , searchsort: [ "name", "-name"
@@ -324,6 +328,8 @@ exports.shorthands =
324 328
   , porcelain : ["--parseable"]
325 329
   , g : ["--global"]
326 330
   , S : ["--save"]
  331
+  , D : ["--save-dev"]
  332
+  , O : ["--save-optional"]
327 333
   , y : ["--yes"]
328 334
   , n : ["--no-yes"]
329 335
   }

0 notes on commit 21266a1

Please sign in to comment.
Something went wrong with that request. Please try again.