@@ -8,8 +8,9 @@ description: Create a package.json file
8
8
9
9
``` bash
10
10
npm init [--force| -f| --yes| -y| --scope]
11
- npm init < @scope> (same as ` npx < @scope> /create` )
12
- npm init [< @scope> /]< name> (same as ` npx [< @scope> /]create-< name> ` )
11
+ npm init < @scope> (same as ` npm exec < @scope> /create` )
12
+ npm init [< @scope> /]< name> (same as ` npm exec [< @scope> /]create-< name> ` )
13
+ npm init [-w < dir> ] [args...]
13
14
```
14
15
15
16
### Description
@@ -18,19 +19,19 @@ npm init [<@scope>/]<name> (same as `npx [<@scope>/]create-<name>`)
18
19
package.
19
20
20
21
` initializer ` in this case is an npm package named ` create-<initializer> ` ,
21
- which will be installed by [ ` npx ` ] ( https:/ /npm.im/npx ) , and then have its
22
+ which will be installed by [ ` npm-exec ` ] ( /commands /npm-exec ) , and then have its
22
23
main bin executed -- presumably creating or updating ` package.json ` and
23
24
running any other initialization-related operations.
24
25
25
- The init command is transformed to a corresponding ` npx ` operation as
26
+ The init command is transformed to a corresponding ` npm exec ` operation as
26
27
follows:
27
28
28
- * ` npm init foo ` -> ` npx create-foo`
29
- * ` npm init @usr/foo ` -> ` npx @usr/create-foo`
30
- * ` npm init @usr ` -> ` npx @usr/create`
29
+ * ` npm init foo ` -> ` npm exec create-foo`
30
+ * ` npm init @usr/foo ` -> ` npm exec @usr/create-foo`
31
+ * ` npm init @usr ` -> ` npm exec @usr/create`
31
32
32
33
Any additional options will be passed directly to the command, so `npm init
33
- foo -- --hello` will map to ` npx create-foo --hello`.
34
+ foo -- --hello` will map to ` npm exec -- create-foo --hello`.
34
35
35
36
If the initializer is omitted (by just calling ` npm init ` ), init will fall
36
37
back to legacy init behavior. It will ask you a bunch of questions, and
@@ -71,6 +72,68 @@ Generate it without having it ask any questions:
71
72
$ npm init -y
72
73
```
73
74
75
+ ### Workspaces support
76
+
77
+ It's possible to create a new workspace within your project by using the
78
+ ` workspace ` config option. When using ` npm init -w <dir> ` the cli will
79
+ create the folders and boilerplate expected while also adding a reference
80
+ to your project ` package.json ` ` "workspaces": [] ` property in order to make
81
+ sure that new generated ** workspace** is properly set up as such.
82
+
83
+ Given a project with no workspaces, e.g:
84
+
85
+ ```
86
+ .
87
+ +-- package.json
88
+ ```
89
+
90
+ You may generate a new workspace using the legacy init:
91
+
92
+ ``` bash
93
+ $ npm init -w packages/a
94
+ ```
95
+
96
+ That will generate a new folder and ` package.json ` file, while also updating
97
+ your top-level ` package.json ` to add the reference to this new workspace:
98
+
99
+ ```
100
+ .
101
+ +-- package.json
102
+ `-- packages
103
+ `-- a
104
+ `-- package.json
105
+ ```
106
+
107
+ The workspaces init also supports the ` npm init <initializer> -w <dir> `
108
+ syntax, following the same set of rules explained earlier in the initial
109
+ ** Description** section of this page. Similar to the previous example of
110
+ creating a new React-based project using
111
+ [ ` create-react-app ` ] ( https://npm.im/create-react-app ) , the following syntax
112
+ will make sure to create the new react app as a nested ** workspace** within your
113
+ project and configure your ` package.json ` to recognize it as such:
114
+
115
+ ``` bash
116
+ npm init -w packages/my-react-app react-app .
117
+ ```
118
+
119
+ This will make sure to generate your react app as expected, one important
120
+ consideration to have in mind is that ` npm exec ` is going to be run in the
121
+ context of the newly created folder for that workspace, and that's the reason
122
+ why in this example the initializer uses the initializer name followed with a
123
+ dot to represent the current directory in that context, e.g: ` react-app . ` :
124
+
125
+ ```
126
+ .
127
+ +-- package.json
128
+ `-- packages
129
+ +-- a
130
+ | `-- package.json
131
+ `-- my-react-app
132
+ +-- README
133
+ +-- package.json
134
+ `-- ...
135
+ ```
136
+
74
137
### A note on caching
75
138
76
139
The npm cli utilizes its internal package cache when using the package
@@ -93,10 +156,38 @@ requested from the server. To force full offline mode, use `offline`.
93
156
Forces full offline mode. Any packages not locally cached will result in
94
157
an error.
95
158
159
+ #### workspace
160
+
161
+ * Alias: ` -w `
162
+ * Type: Array
163
+ * Default: ` [] `
164
+
165
+ Enable running ` npm init ` in the context of workspaces, creating any missing
166
+ folders, generating files and adding/updating the ` "workspaces" ` property of
167
+ the project ` package.json ` .
168
+
169
+ the provided names or paths provided.
170
+
171
+ Valid values for the ` workspace ` config are either:
172
+ - Workspace names
173
+ - Path to a workspace directory
174
+ - Path to a parent workspace directory (will result to selecting all of the
175
+ children workspaces)
176
+
177
+ #### workspaces
178
+
179
+ * Alias: ` -ws `
180
+ * Type: Boolean
181
+ * Default: ` false `
182
+
183
+ Run ` npm init ` in the context of all configured workspaces for the
184
+ current project.
185
+
96
186
### See Also
97
187
98
188
* [ init-package-json module] ( http://npm.im/init-package-json )
99
189
* [ package.json] ( /configuring-npm/package-json )
100
190
* [ npm version] ( /commands/npm-version )
101
191
* [ npm scope] ( /using-npm/scope )
102
192
* [ npm exec] ( /commands/npm-exec )
193
+ * [ npm workspaces] ( /using-npm/workspaces )
0 commit comments