11cb0ef41Sopenharmony_ci---
21cb0ef41Sopenharmony_cititle: npm-version
31cb0ef41Sopenharmony_cisection: 1
41cb0ef41Sopenharmony_cidescription: Bump a package version
51cb0ef41Sopenharmony_ci---
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci### Synopsis
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci```bash
101cb0ef41Sopenharmony_cinpm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cialias: verison
131cb0ef41Sopenharmony_ci```
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci### Configuration
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci#### `allow-same-version`
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci* Default: false
201cb0ef41Sopenharmony_ci* Type: Boolean
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciPrevents throwing an error when `npm version` is used to set the new version
231cb0ef41Sopenharmony_cito the same value as the current version.
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci#### `commit-hooks`
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci* Default: true
301cb0ef41Sopenharmony_ci* Type: Boolean
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciRun git commit hooks when using the `npm version` command.
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci#### `git-tag-version`
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci* Default: true
391cb0ef41Sopenharmony_ci* Type: Boolean
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciTag the commit when using the `npm version` command. Setting this to false
421cb0ef41Sopenharmony_ciresults in no commit being made at all.
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci#### `json`
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci* Default: false
491cb0ef41Sopenharmony_ci* Type: Boolean
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciWhether or not to output JSON data, rather than the normal output.
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci* In `npm pkg set` it enables parsing set values with JSON.parse() before
541cb0ef41Sopenharmony_ci  saving them to your `package.json`.
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciNot supported by all npm commands.
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci#### `preid`
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci* Default: ""
631cb0ef41Sopenharmony_ci* Type: String
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciThe "prerelease identifier" to use as a prefix for the "prerelease" part of
661cb0ef41Sopenharmony_cia semver. Like the `rc` in `1.2.0-rc.8`.
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci#### `sign-git-tag`
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci* Default: false
731cb0ef41Sopenharmony_ci* Type: Boolean
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciIf set to true, then the `npm version` command will tag the version using
761cb0ef41Sopenharmony_ci`-s` to add a signature.
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciNote that git requires you to have set up GPG keys in your git configs for
791cb0ef41Sopenharmony_cithis to work properly.
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci#### `workspace`
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci* Default:
861cb0ef41Sopenharmony_ci* Type: String (can be set multiple times)
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ciEnable running a command in the context of the configured workspaces of the
891cb0ef41Sopenharmony_cicurrent project while filtering by running only the workspaces defined by
901cb0ef41Sopenharmony_cithis configuration option.
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ciValid values for the `workspace` config are either:
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci* Workspace names
951cb0ef41Sopenharmony_ci* Path to a workspace directory
961cb0ef41Sopenharmony_ci* Path to a parent workspace directory (will result in selecting all
971cb0ef41Sopenharmony_ci  workspaces within that folder)
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ciWhen set for the `npm init` command, this may be set to the folder of a
1001cb0ef41Sopenharmony_ciworkspace which does not yet exist, to create the folder and set it up as a
1011cb0ef41Sopenharmony_cibrand new workspace within the project.
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes.
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci#### `workspaces`
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci* Default: null
1081cb0ef41Sopenharmony_ci* Type: null or Boolean
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ciSet to true to run the command in the context of **all** configured
1111cb0ef41Sopenharmony_ciworkspaces.
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ciExplicitly setting this to false will cause commands like `install` to
1141cb0ef41Sopenharmony_ciignore workspaces altogether. When not set explicitly:
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci- Commands that operate on the `node_modules` tree (install, update, etc.)
1171cb0ef41Sopenharmony_ciwill link workspaces into the `node_modules` folder. - Commands that do
1181cb0ef41Sopenharmony_ciother things (test, exec, publish, etc.) will operate on the root project,
1191cb0ef41Sopenharmony_ci_unless_ one or more workspaces are specified in the `workspace` config.
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes.
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci#### `workspaces-update`
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci* Default: true
1261cb0ef41Sopenharmony_ci* Type: Boolean
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ciIf set to true, the npm cli will run an update after operations that may
1291cb0ef41Sopenharmony_cipossibly change the workspaces installed to the `node_modules` folder.
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci#### `include-workspace-root`
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci* Default: false
1361cb0ef41Sopenharmony_ci* Type: Boolean
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ciInclude the workspace root when workspaces are enabled for a command.
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ciWhen false, specifying individual workspaces via the `workspace` config, or
1411cb0ef41Sopenharmony_ciall workspaces via the `workspaces` flag, will cause npm to operate only on
1421cb0ef41Sopenharmony_cithe specified workspaces, and not on the root project.
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes.
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci### Description
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ciRun this in a package directory to bump the version and write the new data
1491cb0ef41Sopenharmony_ciback to `package.json`, `package-lock.json`, and, if present,
1501cb0ef41Sopenharmony_ci`npm-shrinkwrap.json`.
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ciThe `newversion` argument should be a valid semver string, a valid second
1531cb0ef41Sopenharmony_ciargument to [semver.inc](https://github.com/npm/node-semver#functions) (one
1541cb0ef41Sopenharmony_ciof `patch`, `minor`, `major`, `prepatch`, `preminor`, `premajor`,
1551cb0ef41Sopenharmony_ci`prerelease`), or `from-git`. In the second case, the existing version will
1561cb0ef41Sopenharmony_cibe incremented by 1 in the specified field.  `from-git` will try to read
1571cb0ef41Sopenharmony_cithe latest git tag, and use that as the new npm version.
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ciIf run in a git repo, it will also create a version commit and tag.  This
1601cb0ef41Sopenharmony_cibehavior is controlled by `git-tag-version` (see below), and can be
1611cb0ef41Sopenharmony_cidisabled on the command line by running `npm --no-git-tag-version version`.
1621cb0ef41Sopenharmony_ciIt will fail if the working directory is not clean, unless the `-f` or
1631cb0ef41Sopenharmony_ci`--force` flag is set.
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ciIf supplied with `-m` or [`--message` config](/using-npm/config#message) option,
1661cb0ef41Sopenharmony_cinpm will use it as a commit message when creating a version commit.  If the
1671cb0ef41Sopenharmony_ci`message` config contains `%s` then that will be replaced with the resulting
1681cb0ef41Sopenharmony_civersion number. For example:
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci```bash
1711cb0ef41Sopenharmony_cinpm version patch -m "Upgrade to %s for reasons"
1721cb0ef41Sopenharmony_ci```
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ciIf the [`sign-git-tag` config](/using-npm/config#sign-git-tag) is set, then the
1751cb0ef41Sopenharmony_citag will be signed using the `-s` flag to git. Note that you must have a default
1761cb0ef41Sopenharmony_ciGPG key set up in your git config for this to work properly. For example:
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci```bash
1791cb0ef41Sopenharmony_ci$ npm config set sign-git-tag true
1801cb0ef41Sopenharmony_ci$ npm version patch
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ciYou need a passphrase to unlock the secret key for
1831cb0ef41Sopenharmony_ciuser: "isaacs (http://blog.izs.me/) <i@izs.me>"
1841cb0ef41Sopenharmony_ci2048-bit RSA key, ID 6C481CF6, created 2010-08-31
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ciEnter passphrase:
1871cb0ef41Sopenharmony_ci```
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ciIf `preversion`, `version`, or `postversion` are in the `scripts` property
1901cb0ef41Sopenharmony_ciof the package.json, they will be executed as part of running `npm
1911cb0ef41Sopenharmony_civersion`.
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ciThe exact order of execution is as follows:
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci1. Check to make sure the git working directory is clean before we get
1961cb0ef41Sopenharmony_ci   started.  Your scripts may add files to the commit in future steps.
1971cb0ef41Sopenharmony_ci   This step is skipped if the `--force` flag is set.
1981cb0ef41Sopenharmony_ci2. Run the `preversion` script. These scripts have access to the old
1991cb0ef41Sopenharmony_ci   `version` in package.json.  A typical use would be running your full
2001cb0ef41Sopenharmony_ci   test suite before deploying.  Any files you want added to the commit
2011cb0ef41Sopenharmony_ci   should be explicitly added using `git add`.
2021cb0ef41Sopenharmony_ci3. Bump `version` in `package.json` as requested (`patch`, `minor`,
2031cb0ef41Sopenharmony_ci   `major`, etc).
2041cb0ef41Sopenharmony_ci4. Run the `version` script. These scripts have access to the new `version`
2051cb0ef41Sopenharmony_ci   in package.json (so they can incorporate it into file headers in
2061cb0ef41Sopenharmony_ci   generated files for example).  Again, scripts should explicitly add
2071cb0ef41Sopenharmony_ci   generated files to the commit using `git add`.
2081cb0ef41Sopenharmony_ci5. Commit and tag.
2091cb0ef41Sopenharmony_ci6. Run the `postversion` script. Use it to clean up the file system or
2101cb0ef41Sopenharmony_ci   automatically push the commit and/or tag.
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ciTake the following example:
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ci```json
2151cb0ef41Sopenharmony_ci{
2161cb0ef41Sopenharmony_ci  "scripts": {
2171cb0ef41Sopenharmony_ci    "preversion": "npm test",
2181cb0ef41Sopenharmony_ci    "version": "npm run build && git add -A dist",
2191cb0ef41Sopenharmony_ci    "postversion": "git push && git push --tags && rm -rf build/temp"
2201cb0ef41Sopenharmony_ci  }
2211cb0ef41Sopenharmony_ci}
2221cb0ef41Sopenharmony_ci```
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_ciThis runs all your tests and proceeds only if they pass. Then runs your
2251cb0ef41Sopenharmony_ci`build` script, and adds everything in the `dist` directory to the commit.
2261cb0ef41Sopenharmony_ciAfter the commit, it pushes the new commit and tag up to the server, and
2271cb0ef41Sopenharmony_cideletes the `build/temp` directory.
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ci### See Also
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci* [npm init](/commands/npm-init)
2321cb0ef41Sopenharmony_ci* [npm run-script](/commands/npm-run-script)
2331cb0ef41Sopenharmony_ci* [npm scripts](/using-npm/scripts)
2341cb0ef41Sopenharmony_ci* [package.json](/configuring-npm/package-json)
2351cb0ef41Sopenharmony_ci* [config](/using-npm/config)
236