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