Sub topic 7
Generating a Salesforce DX project
This chapter assumes you have installed sf and authorized your Dev Hub, as covered in the earlier chapters. You should also be comfortable reading JSON. Here you create the local project that holds your source code and your scratch org definition. The next chapter edits the definition file.
What a Salesforce DX project is
A Salesforce DX project is a directory with a known layout. At its root is a file named sfdx-project.json. The CLI looks for that file to decide whether a command is running inside a project. It also reads the file for the source folders, the API version, and the login URL.
Source lives in your version control system, not in an org. A scratch org is disposable and can be rebuilt from the project at any time. That only works if the project holds everything needed to recreate your work: metadata, the org definition file, and helper scripts.
Generating the project
Run the generator from the directory where you keep your projects:
sf project generate --name my-project
cd my-projectThe command creates a new folder called my-project under your current directory and fills it with the standard template. The cd matters. Later commands such as sf org create scratch and sf project deploy start expect to run inside the project so they can find sfdx-project.json.
Useful flags
| Flag | Purpose | Example |
|---|---|---|
| `--name` | Project name, also used as the folder name. Required. | `--name my-project` |
| `--template` | `standard` (default), `empty`, or `analytics`. | `--template empty` |
| `--output-dir` | Parent directory in which to create the project folder. | `--output-dir ~/work` |
| `--default-package-dir` | Name of the first source folder. Default is `force-app`. | `--default-package-dir src` |
| `--manifest` | Also generate a `manifest/package.xml` file. | `--manifest` |
| `--namespace` | Namespace to record in `sfdx-project.json`. | `--namespace acme` |
Run sf project generate --help to see the flags your installed version supports. Flags are added over time.
The standard template is the right choice for this tutorial. It includes config/project-scratch-def.json, which the next chapter edits. The empty template creates a smaller project without most of the sample files. Use it only if you plan to supply your own definition file and layout.
What you get
A standard project looks roughly like this. The exact set of tooling files depends on your CLI version.
my-project/
├── config/
│ └── project-scratch-def.json
├── force-app/
│ └── main/
│ └── default/
│ ├── aura/
│ ├── classes/
│ ├── lwc/
│ ├── objects/
│ ├── triggers/
│ └── ...
├── scripts/
│ ├── apex/
│ └── soql/
├── .forceignore
├── .gitignore
├── package.json
└── sfdx-project.jsonEach part has a job:
sfdx-project.jsonis the project configuration. It is the one file the CLI requires.config/project-scratch-def.jsonis the scratch org definition. Its location is only a convention. You pass its path tosf org create scratchwith--definition-file.force-app/main/defaultis where metadata source goes. The nestingmain/defaultis a convention, not a requirement. The CLI treats everything under a package directory as source.scripts/apexandscripts/soqlhold sample anonymous Apex and SOQL files that you can run against an org..forceignorelists files the CLI skips when it deploys or retrieves, in the same style as.gitignore.package.jsoncarries Node.js tooling such as linting and Jest tests for Lightning Web Components. It does not affect deployment. ThesfCLI does not neednpm install.
The block above is intentionally not used. The diagram below shows how a scratch org command uses the project files.
Reading sfdx-project.json
Open the generated file. It looks like this:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"name": "my-project",
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "62.0"
}Your sourceApiVersion will match the API version of your installed CLI, so it may differ from the value shown here.
| Key | Meaning |
|---|---|
| `packageDirectories` | List of folders that contain source. Each entry needs a `path`. |
| `default` | Marks the folder where the CLI puts newly retrieved or generated source. Exactly one entry should be `true`. |
| `name` | Project name. |
| `namespace` | Leave empty unless you own a registered namespace. A value here affects the scratch orgs you create. |
| `sfdcLoginUrl` | Login URL that `sf org login web` uses by default. Set it to `https://test.salesforce.com` for sandboxes, or to a My Domain URL. |
| `sourceApiVersion` | API version used when the CLI deploys and retrieves source. |
Two behaviors are worth knowing:
- The CLI resolves
pathvalues relative to the project root. A path that does not exist on disk causes an error when you deploy or retrieve. - Source from every folder in
packageDirectoriesis included in a deploy. If you add a second folder such asshared, both folders are pushed. Keep a single folder until you have a reason to split.
Putting the project under version control
The generator does not run git init. Do it yourself and commit the baseline:
git init
git add .
git commit -m "Initial Salesforce DX project"The generated .gitignore already excludes the .sf and .sfdx folders. These hold local CLI state, including cached org information, and should never be committed. Commit sfdx-project.json, config/, and force-app/. Your teammates need those to recreate the same scratch org.
Common errors
- Folder already exists. If
my-projectexists in the target directory, the command stops. Choose another name or another--output-dir. - Project generated inside a project. If you run
sf project generatewhile already inside a DX project, you get a nested project. The CLI then uses the innermostsfdx-project.jsonfor commands run from that subfolder. Runpwdbefore you generate. - "This command must be run inside a project." You are not in a folder that has
sfdx-project.jsonin it or above it. Runcd my-projectand retry. - Wrong login URL. If
sf org login webopens the wrong login page, checksfdcLoginUrl. You can also override it for one login with the--instance-urlflag on that command. - Definition file not found. The path you give to
--definition-fileis relative to your current directory, not to the project root. Run the command from the root so thatconfig/project-scratch-def.jsonresolves.
Checking the result
Verify the layout before you move on:
ls
cat sfdx-project.json
cat config/project-scratch-def.jsonAll three should succeed. The definition file the template provides is only a starting point. In the next chapter you replace its contents with your own org name, edition, features, and settings.
Exercise
You generated a project, then ran sf org create scratch --definition-file config/project-scratch-def.json from the parent folder of my-project. The CLI reported that it could not find the file. What is the cause, and what is the fix?
Show answer
The definition file path is resolved relative to the current directory, and you were one level above the project. Run cd my-project first, so that config/project-scratch-def.json exists relative to your location, and then rerun the command.
Exercise
Your team wants the source folder named src instead of force-app. Which command flag sets this at generation time, and which key in sfdx-project.json does it control?
Show answer
Use --default-package-dir src with sf project generate. It sets the path of the package directory entry marked "default": true under packageDirectories.
Exercise
Which folders and files from the generated project should you commit to Git, and which should stay out?
Show answer
Commit sfdx-project.json, config/, force-app/, and .forceignore, so that anyone can rebuild the same scratch org. Keep the .sf and .sfdx folders out of Git because they hold local CLI state. The generated .gitignore already excludes them.