Fast, reliable, and secure dependency management tool for JavaScript/Node.js projects
Commands for project initialization, script execution, and development workflow management.
Create a new package.json file and initialize a new Node.js project.
yarn init [options]
-y, --yes # Accept all defaults without prompting
--private # Mark package as private (not publishable)Usage Examples:
# Interactive initialization
yarn init
# Accept all defaults
yarn init -y
# Create private package
yarn init --private
# Create private package with defaults
yarn init -y --privateThe init command prompts for:
Create a new project using a template package (create-* packages).
yarn create <starter-kit-package> [args]Usage Examples:
# Create React app
yarn create react-app my-app
# Create Next.js app
yarn create next-app my-next-app
# Create Vue app
yarn create vue my-vue-app
# Create with additional arguments
yarn create react-app my-app --template typescript
# Equivalent to running:
# yarn global add create-react-app
# create-react-app my-appExecute scripts defined in package.json or run package binaries.
yarn run <script-name> [args]
yarn <script-name> [args] # Shorthand for common scripts
# Built-in script shortcuts:
yarn start # Runs "start" script
yarn test # Runs "test" script
yarn build # Runs "build" scriptUsage Examples:
# Run custom script
yarn run dev
yarn run build:production
# Run with arguments
yarn run test -- --watch
yarn run build -- --mode production
# Shorthand for common scripts
yarn start
yarn test
yarn build
# List available scripts
yarn run
# Run script in specific workspace
yarn workspace my-package run buildScript Context:
Scripts run with:
node_modules/.bin in PATH.env filesExecute package binaries directly without running them through npm scripts.
yarn exec <command> [args]Usage Examples:
# Execute binary from node_modules/.bin
yarn exec eslint src/
# Execute with arguments
yarn exec webpack --mode production
# Execute binary that might not be in PATH
yarn exec @babel/cli --version
# Equivalent to npx
yarn exec create-react-app my-appRun Node.js with access to node_modules and yarn's environment.
yarn node [args]Usage Examples:
# Run Node.js REPL
yarn node
# Execute JavaScript file
yarn node script.js
# Run with Node.js flags
yarn node --inspect script.js
yarn node --experimental-modules index.mjs
# Access to local modules
yarn node -e "console.log(require('lodash').version)"When running scripts, yarn provides:
# Package information
npm_package_name # Package name from package.json
npm_package_version # Package version
npm_package_description # Package description
# Script context
npm_lifecycle_event # Current script name being run
npm_lifecycle_script # Current script command
# Paths
npm_config_cache # Yarn cache directory
npm_config_prefix # Global install directory
INIT_CWD # Original working directoryYarn supports lifecycle scripts that run automatically:
{
"scripts": {
"preinstall": "echo 'Before install'",
"postinstall": "echo 'After install'",
"prestart": "echo 'Before start'",
"start": "node server.js",
"poststart": "echo 'After start'",
"pretest": "echo 'Before test'",
"test": "jest",
"posttest": "echo 'After test'"
}
}Lifecycle Order:
preinstall → install → postinstallprestart → start → poststartpretest → test → posttestScripts automatically have access to:
node_modules/.bin{
"scripts": {
"lint": "eslint src/", // Uses locally installed eslint
"format": "prettier --write .", // Uses locally installed prettier
"build": "webpack --mode prod" // Uses locally installed webpack
}
}{
"scripts": {
"clean": "rimraf dist/", // Cross-platform rm -rf
"copy": "cpx 'src/**/*.json' dist/", // Cross-platform cp
"env": "cross-env NODE_ENV=prod node server.js"
}
}{
"scripts": {
"build": "yarn clean && yarn compile && yarn minify",
"test:all": "yarn test:unit && yarn test:integration",
"deploy": "yarn build && yarn test && yarn publish"
}
}{
"scripts": {
"dev": "concurrently 'yarn server' 'yarn client'",
"test:parallel": "npm-run-all test:unit test:integration",
"watch": "npm-run-all --parallel watch:*"
}
}Yarn works seamlessly with common project structures:
# Run script in all workspaces
yarn workspaces run build
# Run script in specific workspace
yarn workspace @company/package-a run test# TypeScript compilation
yarn exec tsc
yarn run build # if build script uses tsc
# Type checking
yarn exec tsc --noEmit# ES modules with experimental flag
yarn node --experimental-modules index.mjs
# Module resolution
yarn node --es-module-specifier-resolution=node index.jsInstall with Tessl CLI
npx tessl i tessl/npm-yarn