---
title: Turborepo Codemods
description: To make upgrading easier, Turborepo includes codemods and migration scripts.
---

import Callout from '../../../../components/Callout'

# 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

```sh
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

1. [add-package-manager](#add-package-manager)
2. [create-turbo-config](#create-turbo-config)
3. [migrate-env-var-dependencies](#migrate-env-var-dependencies)
4. [set-default-outputs](#set-default-outputs)

### `add-package-manager`

<Callout type="info">
  Introduced in v1.1.0
</Callout>

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](https://nodejs.org/dist/latest-v17.x/docs/api/packages.html#packagemanager) and is used by Turborepo for faster package manager detection (vs. inferring from just the filesystem alone).

For example, for Yarn v1:

```json
// Before
{
  "name": "turborepo-basic",
  "version": "0.0.0",
  "private": true,
  "workspaces": ["apps/*", "packages/*"]
  // ...
}
```

```diff
{
  "name": "turborepo-basic",
  "version": "0.0.0",
  "private": true,
+  "packageManager": "yarn@1.22.17",
  "workspaces": [
    "apps/*",
    "packages/*"
  ]
}
```

#### Usage

Go to your project:

```sh
cd path-to-your-turborepo/
```

Run the codemod:

```sh
npx @turbo/codemod add-package-manager
```

### `create-turbo-config`

<Callout type="info">
  Introduced in v1.1.0
</Callout>

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:

```json
// Before, package.json
{
  "name": "Monorepo root",
  "private": true,
  "turbo": {
    "pipeline": {
      ...
    }
  },
  ...
}
```

```diff
// 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:

```sh
cd path-to-your-turborepo/
```

Run the codemod:

```sh
npx @turbo/codemod create-turbo-config
```

### `migrate-env-var-dependencies`

<Callout type="info">
  Introduced in v1.5.0
</Callout>

Migrates all environment variable dependencies in `turbo.json` from `dependsOn` and `globalDependencies` to `env` and `globalEnv` respectively.

For example:

```json
// 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
    }
  }
}
```

```diff
// 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:

```sh
cd path-to-your-turborepo/
```

Run the codemod:

```sh
npx @turbo/codemod migrate-env-var-dependencies
```

### `set-default-outputs`

<Callout type="info">
  Introduced in v1.7.0
</Callout>

Migrates `turbo.json` outputs to include the previously inferred `dist/` and `build/`.

For example:

```json
// 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
    }
  }
}
```

```diff
// 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:

```sh
cd path-to-your-turborepo/
```

Run the codemod:

```sh
npx @turbo/codemod set-default-outputs
```
