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