1 ---
2 title: npm-pkg
3 section: 1
4 description: Manages your package.json
5 github_repo: npm/cli
6 github_branch: release/v7
7 github_path: docs/content/commands/npm-pkg.md
8 redirect_from:
9 - /cli-documentation/v7/cli-commands/npm-pkg
10 - /cli-documentation/v7/cli-commands/pkg
11 - /cli-documentation/v7/commands/npm-pkg
12 - /cli-documentation/v7/commands/pkg
13 - /cli-documentation/v7/npm-pkg
14 - /cli-documentation/v7/pkg
15 - /cli/v7/cli-commands/npm-pkg
16 - /cli/v7/cli-commands/pkg
17 - /cli/v7/commands/pkg
18 - /cli/v7/npm-pkg
19 - /cli/v7/pkg
20 ---
21
22 ### Synopsis
23
24 ```bash
25 npm pkg get [<field> [.<subfield> ...]]
26 npm pkg set <field>=<value> [.<subfield>=<value> ...]
27 npm pkg delete <field> [.<subfield> ...]
28 ```
29
30 ### Description
31
32 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`.
33
34 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/v7/commands/npm-view) to retrieve information from the registry manifest, below you can find more examples on how to use it.
35
36 Returned values are always in **json** format.
37
38 - `npm pkg get <field>`
39
40 Retrieves a value `key`, defined in your `package.json` file.
41
42 For example, in order to retrieve the name of the current package, you can run:
43
44 ```bash
45 npm pkg get name
46 ```
47
48 It's also possible to retrieve multiple values at once:
49
50 ```bash
51 npm pkg get name version
52 ```
53
54 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:
55
56 ```bash
57 npm pkg get scripts.test
58 ```
59
60 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:
61
62 ```bash
63 npm pkg get contributors.email
64 ```
65
66 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:
67
68 ```bash
69 npm pkg get contributors[0].email
70 ```
71
72 - `npm pkg set <field>=<value>`
73
74 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.
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. For example, in order to remove a script named build:
119
120 ```bash
121 npm pkg delete scripts.build
122 ```
123
124 ### Workspaces support
125
126 You can set/get/delete items across your configured workspaces by using the `workspace` or `workspaces` config options.
127
128 For example, setting a `funding` value across all configured workspaces of a project:
129
130 ```bash
131 npm pkg set funding=https://example.com --ws
132 ```
133
134 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:
135
136 ```
137 npm pkg get name version --ws
138 {
139 "a": {
140 "name": "a",
141 "version": "1.0.0"
142 },
143 "b": {
144 "name": "b",
145 "version": "1.0.0"
146 }
147 }
148 ```
149
150 ### Configuration
151
152 #### `force`
153
154 - Default: false
155 - Type: Boolean
156
157 Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.
158
159 - Allow clobbering non-npm files in global installs.
160 - Allow the `npm version` command to work on an unclean git repository.
161 - Allow deleting the cache folder with `npm cache clean`.
162 - Allow installing packages that have an `engines` declaration requiring a different version of npm.
163 - Allow installing packages that have an `engines` declaration requiring a different version of `node`, even if `--engine-strict` is enabled.
164 - Allow `npm audit fix` to install modules outside your stated dependency range (including SemVer-major changes).
165 - Allow unpublishing all versions of a published package.
166 - Allow conflicting peerDependencies to be installed in the root project.
167 - Implicitly set `--yes` during `npm init`.
168 - Allow clobbering existing values in `npm pkg`
169
170 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!
171
172 #### `json`
173
174 - Default: false
175 - Type: Boolean
176
177 Whether or not to output JSON data, rather than the normal output.
178
179 - In `npm pkg set` it enables parsing set values with JSON.parse() before saving them to your `package.json`.
180
181 Not supported by all npm commands.
182
183 #### `workspace`
184
185 - Default:
186 - Type: String (can be set multiple times)
187
188 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.
189
190 Valid values for the `workspace` config are either:
191
192 - Workspace names
193 - Path to a workspace directory
194 - Path to a parent workspace directory (will result to selecting all of the nested workspaces)
195
196 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.
197
198 This value is not exported to the environment for child processes.
199
200 #### `workspaces`
201
202 - Default: false
203 - Type: Boolean
204
205 Enable running a command in the context of **all** the configured workspaces.
206
207 This value is not exported to the environment for child processes.
208
209 ## See Also
210
211 - [npm install](/cli/v7/commands/npm-install)
212 - [npm init](/cli/v7/commands/npm-init)
213 - [npm config](/cli/v7/commands/npm-config)
214 - [npm set-script](/cli/v7/commands/npm-set-script)
215 - [workspaces](/cli/v7/using-npm/workspaces)