Sub topic 8
The scratch org definition file
This chapter assumes you can generate a Salesforce DX project, read sfdx-project.json, and run basic sf commands against an authorized Dev Hub. It covers the scratch org definition file: what it controls, how the CLI finds it, and the errors you will meet most often.
What the file is for
A scratch org is a short-lived Salesforce org that you create from source and configuration. The scratch org definition file is the configuration part. It is a JSON file that tells Salesforce what kind of org to build: which edition, which features, and which settings.
The generated project puts this file at config/project-scratch-def.json. The location is a convention, not a rule. sfdx-project.json does not reference it, and the CLI does not search for it. You pass its path to the create command every time.
That separation matters. sfdx-project.json describes your source. The definition file describes the org that will receive that source. You can keep several definition files, for example one for daily development and one for testing a feature that needs extra licenses.
A minimal definition file
This is the file from the generated project:
{
"orgName": "My Project Scratch Org",
"edition": "Developer",
"features": [],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
}
}
}Each top-level key has a job.
| Key | Purpose | Example value |
|---|---|---|
| `orgName` | The company name shown in the org. | `"My Project Scratch Org"` |
| `edition` | The Salesforce edition of the org. | `"Developer"`, `"Enterprise"`, `"Group"`, `"Professional"` |
| `features` | Optional features to turn on. | `["EnableSetPasswordInApi"]` |
| `settings` | Org preferences, grouped by settings type. | See below |
Two other keys are common. description is a free-text note about the file's purpose. country and language set the org's locale, which affects things like default currency and the language of standard labels.
Edition
The edition value decides which standard objects, limits, and licenses the org starts with. Developer is the usual default because it includes most features and has generous limits. Choose a different edition when you need to reproduce production behavior. For example, if your production org is Enterprise Edition, build the scratch org as Enterprise so that limits and available objects match.
If you omit edition, creation fails. The edition is the one key you cannot leave out.
Features
The features array switches on capabilities that are not part of the edition by default. Values are strings, and a few accept a quantity after a colon:
{
"orgName": "Feature Test Org",
"edition": "Developer",
"features": ["Communities", "ServiceCloud", "Walkthroughs", "MultiCurrency", "AuthorApex"],
"settings": {}
}Feature names are case-insensitive but must match Salesforce's list exactly. A typo produces an error at creation time, not a silent skip. Some features also require a matching setting. Enabling Communities in features, for instance, is usually paired with communitiesSettings in settings.
Settings
The settings object holds org preferences. Each key is a settings type, and its value is an object of preference names. The names come from the Metadata API settings types, so a preference you can find in the Metadata API documentation as LightningExperienceSettings.enableS1DesktopEnabled appears here as shown in the minimal file.
{
"orgName": "Dev Org With Settings",
"edition": "Developer",
"features": [],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
},
"securitySettings": {
"passwordPolicies": {
"enableSetPasswordInApi": true
}
},
"mobileSettings": {
"enableS1EncryptedStoragePref2": false
}
}
}Settings keys start with a lowercase letter, unlike the Metadata API type names, which are capitalized. Nested structures follow the same pattern as the Metadata API.
Settings in the definition file are applied once, when the org is created. Changing the file later does not alter an existing scratch org.
Creating a scratch org from the file
Run the create command from the project root, so that the relative path resolves:
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-default --duration-days 7The flags work like this:
--definition-filepoints to the JSON file.--aliasgives the org a short name you can use in later commands.--set-defaultmakes it the default target org for the project.--duration-dayssets how long the org lives, from 1 to 30. The default is 7.
The command needs a Dev Hub, either set as the default with sf config set target-dev-hub or passed with --target-dev-hub. If the Dev Hub is missing, the error names the missing flag.
You can also override a few definition values from the command line. --edition replaces edition, and --no-namespace creates the org without applying your project's namespace. Use overrides for one-off cases and keep the file as the source of truth.
Connecting the org to the project
After creation, the project and the org meet in the deploy and retrieve commands. The definition file shapes the org. sfdx-project.json shapes what you push into it.
sf project deploy start
sf project retrieve startWith a default org set, these commands deploy or retrieve the source in your package directories. Because the scratch org has source tracking, the CLI sends only files that changed. The definition file affects the result indirectly. If your source depends on a feature or setting that the org lacks, the deploy fails even though the source is valid.
Namespaces and the definition file
If sfdx-project.json sets a namespace, a scratch org created from the project uses that namespace by default. That is what you want when you develop a managed package. For unlocked packages without a namespace, leave namespace empty in the project file and nothing extra is applied.
A definition file can also set "noNamespace": true to create the org without the project namespace. This is useful when you want to test code in a plain org.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error that the file cannot be parsed | Invalid JSON, such as a trailing comma or a comment | Validate the JSON and remove comments. |
| Error about a missing edition | `edition` is absent | Add an `edition` key. |
| Error naming an unknown feature | Typo or an unsupported feature name | Check the feature name against the Salesforce documentation. |
| Error naming an unknown setting | Wrong settings type or preference name | Compare with the Metadata API settings type, and start the key lowercase. |
| File not found | Command run outside the project root, or wrong path | Run from the root or pass the correct path. |
| Deploy fails after creation with missing-feature errors | The source needs a feature the definition lacks | Add the feature and create a new org. |
The last row shows a practical rule: because settings and features are applied at creation, fixing the definition file means creating a new scratch org. Delete the old one with sf org delete scratch once you no longer need it, so you stay under the active scratch org limit of your Dev Hub.
Practical habits
- Commit definition files to source control with the rest of the project.
- Name extra files by purpose, such as
config/project-scratch-def-experience.json. - Match the edition and key features to production when you test behavior that depends on them.
- Keep settings minimal. Add a preference only when your source needs it.
Exercise
A teammate edits config/project-scratch-def.json to add a feature, then runs sf project deploy start against their existing scratch org. The deploy still fails because the feature is missing. Why, and what should they do?
Show answer
The definition file is read only when the scratch org is created, so editing it does not change an existing org. They should create a new scratch org with sf org create scratch --definition-file config/project-scratch-def.json, then deploy again, and delete the old org to free a slot on the Dev Hub.
Exercise
Does sfdx-project.json contain the path to the scratch org definition file? How does the CLI know which file to use?
Show answer
No. The path is not stored in sfdx-project.json. You supply it with the --definition-file flag each time you run sf org create scratch.