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