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