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