codekingpro/portable-devtools
114k
1---
2title: npm-version
3section: 1
4description: Bump a package version
5---
6
7### Synopsis
8
9```bash
10npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]
11
12alias: verison
13```
14
15### Configuration
16
17#### `allow-same-version`
18
19* Default: false
20* Type: Boolean
21
22Prevents throwing an error when `npm version` is used to set the new version
23to the same value as the current version.
24
25
26
27#### `commit-hooks`
28
29* Default: true
30* Type: Boolean
31
32Run git commit hooks when using the `npm version` command.
33
34
35
36#### `git-tag-version`
37
38* Default: true
39* Type: Boolean
40
41Tag the commit when using the `npm version` command. Setting this to false
42results in no commit being made at all.
43
44
45
46#### `json`
47
48* Default: false
49* Type: Boolean
50
51Whether or not to output JSON data, rather than the normal output.
52
53* In `npm pkg set` it enables parsing set values with JSON.parse() before
54 saving them to your `package.json`.
55
56Not supported by all npm commands.
57
58
59
60#### `preid`
61
62* Default: ""
63* Type: String
64
65The "prerelease identifier" to use as a prefix for the "prerelease" part of
66a semver. Like the `rc` in `1.2.0-rc.8`.
67
68
69
70#### `sign-git-tag`
71
72* Default: false
73* Type: Boolean
74
75If set to true, then the `npm version` command will tag the version using
76`-s` to add a signature.
77
78Note that git requires you to have set up GPG keys in your git configs for
79this to work properly.
80
81
82
83#### `save`
84
85* Default: `true` unless when using `npm update` where it defaults to `false`
86* Type: Boolean
87
88Save installed packages to a `package.json` file as dependencies.
89
90When used with the `npm rm` command, removes the dependency from
91`package.json`.
92
93Will also prevent writing to `package-lock.json` if set to `false`.
94
95
96
97#### `workspace`
98
99* Default:
100* Type: String (can be set multiple times)
101
102Enable running a command in the context of the configured workspaces of the
103current project while filtering by running only the workspaces defined by
104this configuration option.
105
106Valid values for the `workspace` config are either:
107
108* Workspace names
109* Path to a workspace directory
110* Path to a parent workspace directory (will result in selecting all
111 workspaces within that folder)
112
113When set for the `npm init` command, this may be set to the folder of a
114workspace which does not yet exist, to create the folder and set it up as a
115brand new workspace within the project.
116
117This value is not exported to the environment for child processes.
118
119#### `workspaces`
120
121* Default: null
122* Type: null or Boolean
123
124Set to true to run the command in the context of **all** configured
125workspaces.
126
127Explicitly setting this to false will cause commands like `install` to
128ignore workspaces altogether. When not set explicitly:
129
130- Commands that operate on the `node_modules` tree (install, update, etc.)
131will link workspaces into the `node_modules` folder. - Commands that do
132other things (test, exec, publish, etc.) will operate on the root project,
133_unless_ one or more workspaces are specified in the `workspace` config.
134
135This value is not exported to the environment for child processes.
136
137#### `workspaces-update`
138
139* Default: true
140* Type: Boolean
141
142If set to true, the npm cli will run an update after operations that may
143possibly change the workspaces installed to the `node_modules` folder.
144
145
146
147#### `include-workspace-root`
148
149* Default: false
150* Type: Boolean
151
152Include the workspace root when workspaces are enabled for a command.
153
154When false, specifying individual workspaces via the `workspace` config, or
155all workspaces via the `workspaces` flag, will cause npm to operate only on
156the specified workspaces, and not on the root project.
157
158This value is not exported to the environment for child processes.
159
160#### `ignore-scripts`
161
162* Default: false
163* Type: Boolean
164
165If true, npm does not run scripts specified in package.json files.
166
167Note that commands explicitly intended to run a particular script, such as
168`npm start`, `npm stop`, `npm restart`, `npm test`, and `npm run` will still
169run their intended script if `ignore-scripts` is set, but they will *not*
170run any pre- or post-scripts.
171
172
173
174### Description
175
176Run this in a package directory to bump the version and write the new data back to `package.json`, `package-lock.json`, and, if present,
177`npm-shrinkwrap.json`.
178
179The `newversion` argument should be a valid semver string, a valid second argument to [semver.inc](https://github.com/npm/node-semver#functions) (one of `patch`, `minor`, `major`, `prepatch`, `preminor`, `premajor`, `prerelease`), or `from-git`.
180In the second case, the existing version will be incremented by 1 in the specified field.
181`from-git` will try to read the latest git tag, and use that as the new npm version.
182
183**Note:** If the current version is a prerelease version, `patch` will simply remove the prerelease suffix without incrementing the patch version number. For example, `1.2.0-5` becomes `1.2.0` with `npm version patch`, not `1.2.1`.
184
185If run in a git repo, it will also create a version commit and tag.
186This behavior is controlled by `git-tag-version` (see below), and can be disabled on the command line by running `npm --no-git-tag-version version`.
187It will fail if the working directory is not clean, unless the `-f` or `--force` flag is set.
188
189**Note:** Git integration requires a reasonably recent version of git (2.0.0 or later is recommended). If you encounter issues with git commands, ensure your git installation is up to date.
190
191If supplied with `-m` or [`--message` config](/using-npm/config#message) option, npm will use it as a commit message when creating a version commit.
192If the `message` config contains `%s` then that will be replaced with the resulting version number.
193For example:
194
195```bash
196npm version patch -m "Upgrade to %s for reasons"
197```
198
199If the [`sign-git-tag` config](/using-npm/config#sign-git-tag) is set, then the tag will be signed using the `-s` flag to git.
200Note that you must have a default GPG key set up in your git config for this to work properly.
201For example:
202
203```bash
204$ npm config set sign-git-tag true
205$ npm version patch
206
207You need a passphrase to unlock the secret key for user: "isaacs (http://blog.izs.me/) <i@izs.me>"
2082048-bit RSA key, ID 6C481CF6, created 2010-08-31
209
210Enter passphrase:
211```
212
213If `preversion`, `version`, or `postversion` are in the `scripts` property of the package.json, they will be executed as part of running `npm version`.
214
215The exact order of execution is as follows:
216
2171. Check to make sure the git working directory is clean before we get started.
218 Your scripts may add files to the commit in future steps.
219 This step is skipped if the `--force` flag is set.
2202. Run the `preversion` script.
221 These scripts have access to the old `version` in package.json.
222 A typical use would be running your full test suite before deploying.
223 Any files you want added to the commit should be explicitly added using `git add`.
2243. Bump `version` in `package.json` as requested (`patch`, `minor`, `major`, etc).
2254. Run the `version` script.
226 These scripts have access to the new `version` in package.json (so they can incorporate it into file headers in generated files for example).
227 Again, scripts should explicitly add generated files to the commit using `git add`.
2285. Commit and tag.
2296. Run the `postversion` script.
230 Use it to clean up the file system or automatically push the commit and/or tag.
231
232Take the following example:
233
234```json
235{
236 "scripts": {
237 "preversion": "npm test",
238 "version": "npm run build && git add -A dist",
239 "postversion": "git push && git push --tags && rm -rf build/temp"
240 }
241}
242```
243
244This runs all your tests and proceeds only if they pass.
245Then runs your `build` script, and adds everything in the `dist` directory to the commit.
246After the commit, it pushes the new commit and tag up to the server, and deletes the `build/temp` directory.
247
248### See Also
249
250* [npm init](/commands/npm-init)
251* [npm run](/commands/npm-run)
252* [npm scripts](/using-npm/scripts)
253* [package.json](/configuring-npm/package-json)
254* [config](/using-npm/config)
255 