0
# Project Management
1
2
Commands for project initialization, script execution, and development workflow management.
3
4
## Capabilities
5
6
### Initialize Project
7
8
Create a new package.json file and initialize a new Node.js project.
9
10
```bash { .api }
11
yarn init [options]
12
13
-y, --yes # Accept all defaults without prompting
14
--private # Mark package as private (not publishable)
15
```
16
17
**Usage Examples:**
18
19
```bash
20
# Interactive initialization
21
yarn init
22
23
# Accept all defaults
24
yarn init -y
25
26
# Create private package
27
yarn init --private
28
29
# Create private package with defaults
30
yarn init -y --private
31
```
32
33
The init command prompts for:
34
- Package name
35
- Version (default: 1.0.0)
36
- Description
37
- Entry point (default: index.js)
38
- Repository URL
39
- Author
40
- License (default: MIT)
41
- Private status
42
43
### Create Project from Template
44
45
Create a new project using a template package (create-* packages).
46
47
```bash { .api }
48
yarn create <starter-kit-package> [args]
49
```
50
51
**Usage Examples:**
52
53
```bash
54
# Create React app
55
yarn create react-app my-app
56
57
# Create Next.js app
58
yarn create next-app my-next-app
59
60
# Create Vue app
61
yarn create vue my-vue-app
62
63
# Create with additional arguments
64
yarn create react-app my-app --template typescript
65
66
# Equivalent to running:
67
# yarn global add create-react-app
68
# create-react-app my-app
69
```
70
71
### Run Scripts
72
73
Execute scripts defined in package.json or run package binaries.
74
75
```bash { .api }
76
yarn run <script-name> [args]
77
yarn <script-name> [args] # Shorthand for common scripts
78
79
# Built-in script shortcuts:
80
yarn start # Runs "start" script
81
yarn test # Runs "test" script
82
yarn build # Runs "build" script
83
```
84
85
**Usage Examples:**
86
87
```bash
88
# Run custom script
89
yarn run dev
90
yarn run build:production
91
92
# Run with arguments
93
yarn run test -- --watch
94
yarn run build -- --mode production
95
96
# Shorthand for common scripts
97
yarn start
98
yarn test
99
yarn build
100
101
# List available scripts
102
yarn run
103
104
# Run script in specific workspace
105
yarn workspace my-package run build
106
```
107
108
**Script Context:**
109
110
Scripts run with:
111
- `node_modules/.bin` in PATH
112
- Access to all installed package binaries
113
- Environment variables from `.env` files
114
- Current working directory as project root
115
116
### Execute Binaries
117
118
Execute package binaries directly without running them through npm scripts.
119
120
```bash { .api }
121
yarn exec <command> [args]
122
```
123
124
**Usage Examples:**
125
126
```bash
127
# Execute binary from node_modules/.bin
128
yarn exec eslint src/
129
130
# Execute with arguments
131
yarn exec webpack --mode production
132
133
# Execute binary that might not be in PATH
134
yarn exec @babel/cli --version
135
136
# Equivalent to npx
137
yarn exec create-react-app my-app
138
```
139
140
### Run Node.js
141
142
Run Node.js with access to node_modules and yarn's environment.
143
144
```bash { .api }
145
yarn node [args]
146
```
147
148
**Usage Examples:**
149
150
```bash
151
# Run Node.js REPL
152
yarn node
153
154
# Execute JavaScript file
155
yarn node script.js
156
157
# Run with Node.js flags
158
yarn node --inspect script.js
159
yarn node --experimental-modules index.mjs
160
161
# Access to local modules
162
yarn node -e "console.log(require('lodash').version)"
163
```
164
165
### Script Execution Context
166
167
When running scripts, yarn provides:
168
169
#### Environment Variables
170
171
```bash
172
# Package information
173
npm_package_name # Package name from package.json
174
npm_package_version # Package version
175
npm_package_description # Package description
176
177
# Script context
178
npm_lifecycle_event # Current script name being run
179
npm_lifecycle_script # Current script command
180
181
# Paths
182
npm_config_cache # Yarn cache directory
183
npm_config_prefix # Global install directory
184
INIT_CWD # Original working directory
185
```
186
187
#### Script Hooks
188
189
Yarn supports lifecycle scripts that run automatically:
190
191
```json
192
{
193
"scripts": {
194
"preinstall": "echo 'Before install'",
195
"postinstall": "echo 'After install'",
196
"prestart": "echo 'Before start'",
197
"start": "node server.js",
198
"poststart": "echo 'After start'",
199
"pretest": "echo 'Before test'",
200
"test": "jest",
201
"posttest": "echo 'After test'"
202
}
203
}
204
```
205
206
**Lifecycle Order:**
207
1. `preinstall` → `install` → `postinstall`
208
2. `prestart` → `start` → `poststart`
209
3. `pretest` → `test` → `posttest`
210
4. And so on for other scripts
211
212
#### Binary Access
213
214
Scripts automatically have access to:
215
- All binaries from installed packages in `node_modules/.bin`
216
- Global yarn binaries
217
- System PATH binaries
218
219
```json
220
{
221
"scripts": {
222
"lint": "eslint src/", // Uses locally installed eslint
223
"format": "prettier --write .", // Uses locally installed prettier
224
"build": "webpack --mode prod" // Uses locally installed webpack
225
}
226
}
227
```
228
229
### Script Best Practices
230
231
#### Cross-platform Commands
232
233
```json
234
{
235
"scripts": {
236
"clean": "rimraf dist/", // Cross-platform rm -rf
237
"copy": "cpx 'src/**/*.json' dist/", // Cross-platform cp
238
"env": "cross-env NODE_ENV=prod node server.js"
239
}
240
}
241
```
242
243
#### Chaining Commands
244
245
```json
246
{
247
"scripts": {
248
"build": "yarn clean && yarn compile && yarn minify",
249
"test:all": "yarn test:unit && yarn test:integration",
250
"deploy": "yarn build && yarn test && yarn publish"
251
}
252
}
253
```
254
255
#### Parallel Execution
256
257
```json
258
{
259
"scripts": {
260
"dev": "concurrently 'yarn server' 'yarn client'",
261
"test:parallel": "npm-run-all test:unit test:integration",
262
"watch": "npm-run-all --parallel watch:*"
263
}
264
}
265
```
266
267
## Project Structure Integration
268
269
Yarn works seamlessly with common project structures:
270
271
### Monorepo/Workspace Projects
272
273
```bash
274
# Run script in all workspaces
275
yarn workspaces run build
276
277
# Run script in specific workspace
278
yarn workspace @company/package-a run test
279
```
280
281
### TypeScript Projects
282
283
```bash
284
# TypeScript compilation
285
yarn exec tsc
286
yarn run build # if build script uses tsc
287
288
# Type checking
289
yarn exec tsc --noEmit
290
```
291
292
### Modern JavaScript Projects
293
294
```bash
295
# ES modules with experimental flag
296
yarn node --experimental-modules index.mjs
297
298
# Module resolution
299
yarn node --es-module-specifier-resolution=node index.js
300
```