Turborepo Codemods
Turborepo provides Codemod transformations and automatic migration scripts to help upgrade your Turborepo codebase when a feature is deprecated.
Codemods are transformations that run on your codebase programmatically. This allows for a large amount of changes to be applied without having to manually go through every file.
Usage
npx @turbo/codemod <transform> <path>transform- name of transform, see available transforms below.path- files or directory to transform--dry- Do a dry-run, no code will be edited--print- Prints the changed output for comparison
Turborepo 1.x
- add-package-manager
- create-turbo-config
- migrate-env-var-dependencies
- set-default-outputs
- stabilize-env-mode
- transform-env-literals-to-wildcards
add-package-manager
Introduced in v1.1.0
Transforms the root package.json so that packageManager key as the detected package manager (yarn, npm, pnpm) and version (e.g. yarn@1.22.17). This key is now supported by Node.js (opens in a new tab) and is used by Turborepo for faster package manager detection (vs. inferring from just the filesystem alone).
For example, for Yarn v1:
// Before
{
"name": "turborepo-basic",
"version": "0.0.0",
"private": true,
"workspaces": ["apps/*", "packages/*"]
// ...
}{
"name": "turborepo-basic",
"version": "0.0.0",
"private": true,
+ "packageManager": "yarn@1.22.17",
"workspaces": [
"apps/*",
"packages/*"
]
}Usage
Go to your project:
cd path-to-your-turborepo/Run the codemod:
npx @turbo/codemod add-package-managercreate-turbo-config
Introduced in v1.1.0
Creates the turbo.json file at the root of your project based on the "turbo" key in package.json.
The "turbo" key is subsequently deleted from package.json.
For example:
// Before, package.json
{
"name": "Monorepo root",
"private": true,
"turbo": {
"pipeline": {
...
}
},
...
}// After, package.json
{
"name": "Monorepo root",
"private": true,
- "turbo": {
- "pipeline": {
- ...
- }
- },
...
}
// After, turbo.json
+{
+ "$schema": "https://turbo.build/schema.json",
+ "pipeline": {
+ ...
+ }
+}Usage
Go to your project:
cd path-to-your-turborepo/Run the codemod:
npx @turbo/codemod create-turbo-configmigrate-env-var-dependencies
Introduced in v1.5.0
Migrates all environment variable dependencies in turbo.json from dependsOn and globalDependencies to env and globalEnv respectively.
For example:
// Before, turbo.json
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": [".env", "$CI_ENV"],
"pipeline": {
"build": {
"dependsOn": ["^build", "$API_BASE"],
"outputs": [".next/**", "!.next/cache/**"]
},
"lint": {},
"dev": {
"cache": false,
"persistent": true
}
}
}// After, turbo.json
{
"$schema": "https://turbo.build/schema.json",
- "globalDependencies": [".env", "$CI_ENV"],
+ "globalDependencies": [".env"],
+ "globalEnv": ["CI_ENV"],
"pipeline": {
"build": {
- "dependsOn": ["^build", "$API_BASE"],
+ "dependsOn": ["^build"],
+ "env": ["API_BASE"],
"outputs": [".next/**", "!.next/cache/**"],
},
"lint": {},
"dev": {
"cache": false,
"persistent": true
}
}
}Usage
Go to your project:
cd path-to-your-turborepo/Run the codemod:
npx @turbo/codemod migrate-env-var-dependenciesset-default-outputs
Introduced in v1.7.0
Migrates turbo.json outputs to include the previously inferred dist/ and build/.
For example:
// Before, turbo.json
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": [".env"],
"globalEnv": ["CI_ENV"],
"pipeline": {
"build": {
"dependsOn": ["^build"],
"env": ["API_BASE"],
"outputs": [".next/**", "!.next/cache/**"]
},
"lint": {
"outputs": []
},
"dev": {
"cache": false,
"persistent": true
}
}
}// After, turbo.json
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": [".env"],
"globalEnv": ["CI_ENV"],
"pipeline": {
"build": {
"dependsOn": ["^build"],
"env": ["API_BASE"],
"outputs": [".next/**", "!.next/cache/**"]
},
- "lint": {
- "outputs": []
- },
+ "lint": {},
"dev": {
"cache": false,
"persistent": true,
+ "outputs": ["dist/**", "build/**"]
}
}
}
Usage
Go to your project:
cd path-to-your-turborepo/Run the codemod:
npx @turbo/codemod set-default-outputsstabilize-env-mode
Introduced in v1.10.0
Migrates turbo.json's experimentalGlobalPassThroughEnv to globalPassThroughEnv and experimentalPassThroughEnv to passThroughEnv.
For example:
// Before, turbo.json
{
"$schema": "https://turbo.build/schema.json",
"experimentalGlobalPassThroughEnv": ["CC"],
"pipeline": {
"build": {
"experimentalPassThroughEnv": ["GOROOT"],
}
}
}// After, turbo.json
{
"$schema": "https://turbo.build/schema.json",
"globalPassThroughEnv": ["CC"],
"pipeline": {
"build": {
"passThroughEnv": ["GOROOT"],
}
}
}Usage
Go to your project:
cd path-to-your-turborepo/Run the codemod:
npx @turbo/codemod stabilize-env-modetransform-env-literals-to-wildcards
Introduced in v1.10.0
Updates any existing env var fields whose contents would be ambiguous to the new wildcard syntax.
For example:
// Before, turbo.json
{
"$schema": "https://turbo.build/schema.json",
"globalEnv": ["THIS_*_IS_LITERAL"],
"globalPassThroughEnv": ["!LITERAL_LEADING_EXCLAMATION"],
"pipeline": {
"build": {
"env": ["50_PERCENT_OFF*_HAS_SMALL_PRINT"],
"passThroughEnv": ["**BOLDED**"],
}
}
}// After, turbo.json
{
"$schema": "https://turbo.build/schema.json",
"globalEnv": ["THIS_\\*_IS_LITERAL"],
"globalPassThroughEnv": ["\\!LITERAL_LEADING_EXCLAMATION"],
"pipeline": {
"build": {
"env": ["50_PERCENT_OFF\\*_HAS_SMALL_PRINT"],
"passThroughEnv": ["\\*\\*BOLDED\\*\\*"],
}
}
}Usage
Go to your project:
cd path-to-your-turborepo/Run the codemod:
npx @turbo/codemod transform-env-literals-to-wildcards