11cb0ef41Sopenharmony_ci--- 21cb0ef41Sopenharmony_cititle: npm-pkg 31cb0ef41Sopenharmony_cisection: 1 41cb0ef41Sopenharmony_cidescription: Manages your package.json 51cb0ef41Sopenharmony_ci--- 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci### Synopsis 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci```bash 101cb0ef41Sopenharmony_cinpm pkg set <key>=<value> [<key>=<value> ...] 111cb0ef41Sopenharmony_cinpm pkg get [<key> [<key> ...]] 121cb0ef41Sopenharmony_cinpm pkg delete <key> [<key> ...] 131cb0ef41Sopenharmony_cinpm pkg set [<array>[<index>].<key>=<value> ...] 141cb0ef41Sopenharmony_cinpm pkg set [<array>[].<key>=<value> ...] 151cb0ef41Sopenharmony_cinpm pkg fix 161cb0ef41Sopenharmony_ci``` 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci### Description 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ciA command that automates the management of `package.json` files. 211cb0ef41Sopenharmony_ci`npm pkg` provide 3 different sub commands that allow you to modify or retrieve 221cb0ef41Sopenharmony_civalues for given object keys in your `package.json`. 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ciThe syntax to retrieve and set fields is a dot separated representation of 251cb0ef41Sopenharmony_cithe nested object properties to be found within your `package.json`, it's the 261cb0ef41Sopenharmony_cisame notation used in [`npm view`](/commands/npm-view) to retrieve information 271cb0ef41Sopenharmony_cifrom the registry manifest, below you can find more examples on how to use it. 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciReturned values are always in **json** format. 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci* `npm pkg get <field>` 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci Retrieves a value `key`, defined in your `package.json` file. 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci For example, in order to retrieve the name of the current package, you 361cb0ef41Sopenharmony_ci can run: 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci ```bash 391cb0ef41Sopenharmony_ci npm pkg get name 401cb0ef41Sopenharmony_ci ``` 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci It's also possible to retrieve multiple values at once: 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci ```bash 451cb0ef41Sopenharmony_ci npm pkg get name version 461cb0ef41Sopenharmony_ci ``` 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci You can view child fields by separating them with a period. To retrieve 491cb0ef41Sopenharmony_ci the value of a test `script` value, you would run the following command: 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci ```bash 521cb0ef41Sopenharmony_ci npm pkg get scripts.test 531cb0ef41Sopenharmony_ci ``` 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci For fields that are arrays, requesting a non-numeric field will return 561cb0ef41Sopenharmony_ci all of the values from the objects in the list. For example, to get all 571cb0ef41Sopenharmony_ci the contributor emails for a package, you would run: 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci ```bash 601cb0ef41Sopenharmony_ci npm pkg get contributors.email 611cb0ef41Sopenharmony_ci ``` 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci You may also use numeric indices in square braces to specifically select 641cb0ef41Sopenharmony_ci an item in an array field. To just get the email address of the first 651cb0ef41Sopenharmony_ci contributor in the list, you can run: 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci ```bash 681cb0ef41Sopenharmony_ci npm pkg get contributors[0].email 691cb0ef41Sopenharmony_ci ``` 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci For complex fields you can also name a property in square brackets 721cb0ef41Sopenharmony_ci to specifically select a child field. This is especially helpful 731cb0ef41Sopenharmony_ci with the exports object: 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci ```bash 761cb0ef41Sopenharmony_ci npm pkg get "exports[.].require" 771cb0ef41Sopenharmony_ci ``` 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci* `npm pkg set <field>=<value>` 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci Sets a `value` in your `package.json` based on the `field` value. When 821cb0ef41Sopenharmony_ci saving to your `package.json` file the same set of rules used during 831cb0ef41Sopenharmony_ci `npm install` and other cli commands that touches the `package.json` file 841cb0ef41Sopenharmony_ci are used, making sure to respect the existing indentation and possibly 851cb0ef41Sopenharmony_ci applying some validation prior to saving values to the file. 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci The same syntax used to retrieve values from your package can also be used 881cb0ef41Sopenharmony_ci to define new properties or overriding existing ones, below are some 891cb0ef41Sopenharmony_ci examples of how the dot separated syntax can be used to edit your 901cb0ef41Sopenharmony_ci `package.json` file. 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci Defining a new bin named `mynewcommand` in your `package.json` that points 931cb0ef41Sopenharmony_ci to a file `cli.js`: 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci ```bash 961cb0ef41Sopenharmony_ci npm pkg set bin.mynewcommand=cli.js 971cb0ef41Sopenharmony_ci ``` 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci Setting multiple fields at once is also possible: 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci ```bash 1021cb0ef41Sopenharmony_ci npm pkg set description='Awesome package' engines.node='>=10' 1031cb0ef41Sopenharmony_ci ``` 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci It's also possible to add to array values, for example to add a new 1061cb0ef41Sopenharmony_ci contributor entry: 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci ```bash 1091cb0ef41Sopenharmony_ci npm pkg set contributors[0].name='Foo' contributors[0].email='foo@bar.ca' 1101cb0ef41Sopenharmony_ci ``` 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci You may also append items to the end of an array using the special 1131cb0ef41Sopenharmony_ci empty bracket notation: 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci ```bash 1161cb0ef41Sopenharmony_ci npm pkg set contributors[].name='Foo' contributors[].name='Bar' 1171cb0ef41Sopenharmony_ci ``` 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci It's also possible to parse values as json prior to saving them to your 1201cb0ef41Sopenharmony_ci `package.json` file, for example in order to set a `"private": true` 1211cb0ef41Sopenharmony_ci property: 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci ```bash 1241cb0ef41Sopenharmony_ci npm pkg set private=true --json 1251cb0ef41Sopenharmony_ci ``` 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci It also enables saving values as numbers: 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci ```bash 1301cb0ef41Sopenharmony_ci npm pkg set tap.timeout=60 --json 1311cb0ef41Sopenharmony_ci ``` 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci* `npm pkg delete <key>` 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci Deletes a `key` from your `package.json` 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ci The same syntax used to set values from your package can also be used 1381cb0ef41Sopenharmony_ci to remove existing ones. For example, in order to remove a script named 1391cb0ef41Sopenharmony_ci build: 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci ```bash 1421cb0ef41Sopenharmony_ci npm pkg delete scripts.build 1431cb0ef41Sopenharmony_ci ``` 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci* `npm pkg fix` 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci Auto corrects common errors in your `package.json`. npm already 1481cb0ef41Sopenharmony_ci does this during `publish`, which leads to subtle (mostly harmless) 1491cb0ef41Sopenharmony_ci differences between the contents of your `package.json` file and the 1501cb0ef41Sopenharmony_ci manifest that npm uses during installation. 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci### Workspaces support 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ciYou can set/get/delete items across your configured workspaces by using the 1551cb0ef41Sopenharmony_ci[`workspace`](/using-npm/config#workspace) or 1561cb0ef41Sopenharmony_ci[`workspaces`](/using-npm/config#workspaces) config options. 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ciFor example, setting a `funding` value across all configured workspaces 1591cb0ef41Sopenharmony_ciof a project: 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci```bash 1621cb0ef41Sopenharmony_cinpm pkg set funding=https://example.com --ws 1631cb0ef41Sopenharmony_ci``` 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_ciWhen using `npm pkg get` to retrieve info from your configured workspaces, the 1661cb0ef41Sopenharmony_cireturned result will be in a json format in which top level keys are the 1671cb0ef41Sopenharmony_cinames of each workspace, the values of these keys will be the result values 1681cb0ef41Sopenharmony_cireturned from each of the configured workspaces, e.g: 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci``` 1711cb0ef41Sopenharmony_cinpm pkg get name version --ws 1721cb0ef41Sopenharmony_ci{ 1731cb0ef41Sopenharmony_ci "a": { 1741cb0ef41Sopenharmony_ci "name": "a", 1751cb0ef41Sopenharmony_ci "version": "1.0.0" 1761cb0ef41Sopenharmony_ci }, 1771cb0ef41Sopenharmony_ci "b": { 1781cb0ef41Sopenharmony_ci "name": "b", 1791cb0ef41Sopenharmony_ci "version": "1.0.0" 1801cb0ef41Sopenharmony_ci } 1811cb0ef41Sopenharmony_ci} 1821cb0ef41Sopenharmony_ci``` 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci### Configuration 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci#### `force` 1871cb0ef41Sopenharmony_ci 1881cb0ef41Sopenharmony_ci* Default: false 1891cb0ef41Sopenharmony_ci* Type: Boolean 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ciRemoves various protections against unfortunate side effects, common 1921cb0ef41Sopenharmony_cimistakes, unnecessary performance degradation, and malicious input. 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ci* Allow clobbering non-npm files in global installs. 1951cb0ef41Sopenharmony_ci* Allow the `npm version` command to work on an unclean git repository. 1961cb0ef41Sopenharmony_ci* Allow deleting the cache folder with `npm cache clean`. 1971cb0ef41Sopenharmony_ci* Allow installing packages that have an `engines` declaration requiring a 1981cb0ef41Sopenharmony_ci different version of npm. 1991cb0ef41Sopenharmony_ci* Allow installing packages that have an `engines` declaration requiring a 2001cb0ef41Sopenharmony_ci different version of `node`, even if `--engine-strict` is enabled. 2011cb0ef41Sopenharmony_ci* Allow `npm audit fix` to install modules outside your stated dependency 2021cb0ef41Sopenharmony_ci range (including SemVer-major changes). 2031cb0ef41Sopenharmony_ci* Allow unpublishing all versions of a published package. 2041cb0ef41Sopenharmony_ci* Allow conflicting peerDependencies to be installed in the root project. 2051cb0ef41Sopenharmony_ci* Implicitly set `--yes` during `npm init`. 2061cb0ef41Sopenharmony_ci* Allow clobbering existing values in `npm pkg` 2071cb0ef41Sopenharmony_ci* Allow unpublishing of entire packages (not just a single version). 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ciIf you don't have a clear idea of what you want to do, it is strongly 2101cb0ef41Sopenharmony_cirecommended that you do not use this option! 2111cb0ef41Sopenharmony_ci 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ci 2141cb0ef41Sopenharmony_ci#### `json` 2151cb0ef41Sopenharmony_ci 2161cb0ef41Sopenharmony_ci* Default: false 2171cb0ef41Sopenharmony_ci* Type: Boolean 2181cb0ef41Sopenharmony_ci 2191cb0ef41Sopenharmony_ciWhether or not to output JSON data, rather than the normal output. 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ci* In `npm pkg set` it enables parsing set values with JSON.parse() before 2221cb0ef41Sopenharmony_ci saving them to your `package.json`. 2231cb0ef41Sopenharmony_ci 2241cb0ef41Sopenharmony_ciNot supported by all npm commands. 2251cb0ef41Sopenharmony_ci 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ci 2281cb0ef41Sopenharmony_ci#### `workspace` 2291cb0ef41Sopenharmony_ci 2301cb0ef41Sopenharmony_ci* Default: 2311cb0ef41Sopenharmony_ci* Type: String (can be set multiple times) 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ciEnable running a command in the context of the configured workspaces of the 2341cb0ef41Sopenharmony_cicurrent project while filtering by running only the workspaces defined by 2351cb0ef41Sopenharmony_cithis configuration option. 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ciValid values for the `workspace` config are either: 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_ci* Workspace names 2401cb0ef41Sopenharmony_ci* Path to a workspace directory 2411cb0ef41Sopenharmony_ci* Path to a parent workspace directory (will result in selecting all 2421cb0ef41Sopenharmony_ci workspaces within that folder) 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ciWhen set for the `npm init` command, this may be set to the folder of a 2451cb0ef41Sopenharmony_ciworkspace which does not yet exist, to create the folder and set it up as a 2461cb0ef41Sopenharmony_cibrand new workspace within the project. 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes. 2491cb0ef41Sopenharmony_ci 2501cb0ef41Sopenharmony_ci#### `workspaces` 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci* Default: null 2531cb0ef41Sopenharmony_ci* Type: null or Boolean 2541cb0ef41Sopenharmony_ci 2551cb0ef41Sopenharmony_ciSet to true to run the command in the context of **all** configured 2561cb0ef41Sopenharmony_ciworkspaces. 2571cb0ef41Sopenharmony_ci 2581cb0ef41Sopenharmony_ciExplicitly setting this to false will cause commands like `install` to 2591cb0ef41Sopenharmony_ciignore workspaces altogether. When not set explicitly: 2601cb0ef41Sopenharmony_ci 2611cb0ef41Sopenharmony_ci- Commands that operate on the `node_modules` tree (install, update, etc.) 2621cb0ef41Sopenharmony_ciwill link workspaces into the `node_modules` folder. - Commands that do 2631cb0ef41Sopenharmony_ciother things (test, exec, publish, etc.) will operate on the root project, 2641cb0ef41Sopenharmony_ci_unless_ one or more workspaces are specified in the `workspace` config. 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes. 2671cb0ef41Sopenharmony_ci## See Also 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ci* [npm install](/commands/npm-install) 2701cb0ef41Sopenharmony_ci* [npm init](/commands/npm-init) 2711cb0ef41Sopenharmony_ci* [npm config](/commands/npm-config) 2721cb0ef41Sopenharmony_ci* [workspaces](/using-npm/workspaces) 273