codekingpro/portable-devtools
114k
1---
2title: npm-pkg
3section: 1
4description: Manages your package.json
5---
6
7### Synopsis
8
9```bash
10npm pkg set <key>=<value> [<key>=<value> ...]
11npm pkg get [<key> [<key> ...]]
12npm pkg delete <key> [<key> ...]
13npm pkg set [<array>[<index>].<key>=<value> ...]
14npm pkg set [<array>[].<key>=<value> ...]
15npm pkg fix
16```
17
18### Description
19
20A command that automates the management of `package.json` files.
21`npm pkg` provide 3 different sub commands that allow you to modify or retrieve values for given object keys in your `package.json`.
22
23The syntax to retrieve and set fields is a dot separated representation of the nested object properties to be found within your `package.json`, it's the same notation used in [`npm view`](/commands/npm-view) to retrieve information from the registry manifest, below you can find more examples on how to use it.
24
25Returned values are always in **json** format.
26
27* `npm pkg get <field>`
28
29 Retrieves a value `key`, defined in your `package.json` file.
30
31 For example, in order to retrieve the name of the current package, you can run:
32
33 ```bash
34 npm pkg get name
35 ```
36
37 It's also possible to retrieve multiple values at once:
38
39 ```bash
40 npm pkg get name version
41 ```
42
43 You can view child fields by separating them with a period.
44 To retrieve the value of a test `script` value, you would run the following command:
45
46 ```bash
47 npm pkg get scripts.test
48 ```
49
50 For fields that are arrays, requesting a non-numeric field will return all of the values from the objects in the list.
51 For example, to get all the contributor emails for a package, you would run:
52
53 ```bash
54 npm pkg get contributors.email
55 ```
56
57 You may also use numeric indices in square braces to specifically select an item in an array field.
58 To just get the email address of the first contributor in the list, you can run:
59
60 ```bash
61 npm pkg get contributors[0].email
62 ```
63
64 For complex fields you can also name a property in square brackets to specifically select a child field.
65 This is especially helpful with the exports object:
66
67 ```bash
68 npm pkg get "exports[.].require"
69 ```
70
71* `npm pkg set <field>=<value>`
72
73 Sets a `value` in your `package.json` based on the `field` value.
74 When saving to your `package.json` file the same set of rules used during `npm install` and other cli commands that touches the `package.json` file are used, making sure to respect the existing indentation and possibly applying some validation prior to saving values to the file.
75
76 The same syntax used to retrieve values from your package can also be used to define new properties or overriding existing ones, below are some examples of how the dot separated syntax can be used to edit your `package.json` file.
77
78 Defining a new bin named `mynewcommand` in your `package.json` that points to a file `cli.js`:
79
80 ```bash
81 npm pkg set bin.mynewcommand=cli.js
82 ```
83
84 Setting multiple fields at once is also possible:
85
86 ```bash
87 npm pkg set description='Awesome package' engines.node='>=10'
88 ```
89
90 It's also possible to add to array values, for example to add a new contributor entry:
91
92 ```bash
93 npm pkg set contributors[0].name='Foo' contributors[0].email='foo@bar.ca'
94 ```
95
96 You may also append items to the end of an array using the special empty bracket notation:
97
98 ```bash
99 npm pkg set contributors[].name='Foo' contributors[].name='Bar'
100 ```
101
102 It's also possible to parse values as json prior to saving them to your `package.json` file, for example in order to set a `"private": true` property:
103
104 ```bash
105 npm pkg set private=true --json
106 ```
107
108 It also enables saving values as numbers:
109
110 ```bash
111 npm pkg set tap.timeout=60 --json
112 ```
113
114* `npm pkg delete <key>`
115
116 Deletes a `key` from your `package.json`
117
118 The same syntax used to set values from your package can also be used to remove existing ones.
119 For example, in order to remove a script named build:
120
121 ```bash
122 npm pkg delete scripts.build
123 ```
124
125* `npm pkg fix`
126
127 Auto corrects common errors in your `package.json`.
128 npm already does this during `publish`, which leads to subtle (mostly harmless) differences between the contents of your `package.json` file and the manifest that npm uses during installation.
129
130### Workspaces support
131
132You can set/get/delete items across your configured workspaces by using the [`workspace`](/using-npm/config#workspace) or [`workspaces`](/using-npm/config#workspaces) config options.
133
134For example, setting a `funding` value across all configured workspaces of a project:
135
136```bash
137npm pkg set funding=https://example.com --ws
138```
139
140When using `npm pkg get` to retrieve info from your configured workspaces, the returned result will be in a json format in which top level keys are the names of each workspace, the values of these keys will be the result values returned from each of the configured workspaces, e.g:
141
142```
143npm pkg get name version --ws
144{
145 "a": {
146 "name": "a",
147 "version": "1.0.0"
148 },
149 "b": {
150 "name": "b",
151 "version": "1.0.0"
152 }
153}
154```
155
156### Configuration
157
158#### `force`
159
160* Default: false
161* Type: Boolean
162
163Removes various protections against unfortunate side effects, common
164mistakes, unnecessary performance degradation, and malicious input.
165
166* Allow clobbering non-npm files in global installs.
167* Allow the `npm version` command to work on an unclean git repository.
168* Allow deleting the cache folder with `npm cache clean`.
169* Allow installing packages that have an `engines` declaration requiring a
170 different version of npm.
171* Allow installing packages that have an `engines` declaration requiring a
172 different version of `node`, even if `--engine-strict` is enabled.
173* Allow `npm audit fix` to install modules outside your stated dependency
174 range (including SemVer-major changes).
175* Allow unpublishing all versions of a published package.
176* Allow conflicting peerDependencies to be installed in the root project.
177* Implicitly set `--yes` during `npm init`.
178* Allow clobbering existing values in `npm pkg`
179* Allow unpublishing of entire packages (not just a single version).
180
181If you don't have a clear idea of what you want to do, it is strongly
182recommended that you do not use this option!
183
184
185
186#### `json`
187
188* Default: false
189* Type: Boolean
190
191Whether or not to output JSON data, rather than the normal output.
192
193* In `npm pkg set` it enables parsing set values with JSON.parse() before
194 saving them to your `package.json`.
195
196Not supported by all npm commands.
197
198
199
200#### `workspace`
201
202* Default:
203* Type: String (can be set multiple times)
204
205Enable running a command in the context of the configured workspaces of the
206current project while filtering by running only the workspaces defined by
207this configuration option.
208
209Valid values for the `workspace` config are either:
210
211* Workspace names
212* Path to a workspace directory
213* Path to a parent workspace directory (will result in selecting all
214 workspaces within that folder)
215
216When set for the `npm init` command, this may be set to the folder of a
217workspace which does not yet exist, to create the folder and set it up as a
218brand new workspace within the project.
219
220This value is not exported to the environment for child processes.
221
222#### `workspaces`
223
224* Default: null
225* Type: null or Boolean
226
227Set to true to run the command in the context of **all** configured
228workspaces.
229
230Explicitly setting this to false will cause commands like `install` to
231ignore workspaces altogether. When not set explicitly:
232
233- Commands that operate on the `node_modules` tree (install, update, etc.)
234will link workspaces into the `node_modules` folder. - Commands that do
235other things (test, exec, publish, etc.) will operate on the root project,
236_unless_ one or more workspaces are specified in the `workspace` config.
237
238This value is not exported to the environment for child processes.
239## See Also
240
241* [npm install](/commands/npm-install)
242* [npm init](/commands/npm-init)
243* [npm config](/commands/npm-config)
244* [workspaces](/using-npm/workspaces)
245 