Sub topic 2
Naming the Folder and Files
This chapter assumes you know basic JavaScript modules and classes and can read a Salesforce DX project layout. The bundle overview chapter showed that a component is a folder of files. Here you learn the naming rules that tie those files together. Most "my component doesn't show up" problems for new developers come from breaking one of these rules.
One name drives everything
A Lightning web component bundle is a folder directly inside the project's lwc directory. The folder name is the component's name. Every file with a fixed role must use that same name as its base name, which is the part before the extension.
force-app/main/default/lwc/
└── greetingCard/
├── greetingCard.html
├── greetingCard.js
├── greetingCard.css
├── greetingCard.js-meta.xml
├── greetingCard.svg
└── __tests__/
└── greetingCard.test.jsThe platform finds the template, class, styles and configuration by combining the folder name with a known extension. You never write a path that links greetingCard.js to greetingCard.html. The shared name is the link.
The same name also determines three other things:
- The module specifier other JavaScript uses to import the component, such as
c/greetingCard. - The tag other templates use to place it, such as
<c-greeting-card>. - The name of the component in the org's metadata, which is how deployment and source tracking identify it.
Rules for the folder name
The folder name must satisfy all of these:
- It begins with a lowercase letter.
- It contains only letters, digits and underscores.
- It has no whitespace and no hyphens.
- It does not end with an underscore.
- It does not contain two underscores in a row.
- It is unique within its namespace.
The convention is camelCase: lowercase first letter, then a capital at each new word, as in greetingCard or accountSummaryPanel. Names like Greeting_Card, greeting-card, greeting card and _greeting all break a rule.
The hyphen rule surprises people because hyphens appear in the tag. They are not allowed in the folder name. The platform generates the hyphens for you, as the next section shows.
The component folders must also sit directly inside lwc. A bundle cannot be nested inside another bundle's folder or inside a grouping folder like lwc/shared/greetingCard. The only subfolder with a defined meaning inside a bundle is __tests__, covered in a later chapter.
From folder name to tag and import
The namespace is a prefix that keeps your names from colliding with others'. For code in your own org, and for most unmanaged code, the namespace is c. Packaged code uses the package's namespace instead.
The import specifier is the namespace, a slash and the folder name exactly as written:
import GreetingCard from 'c/greetingCard';The markup tag converts camelCase to kebab-case. Each capital letter becomes a hyphen plus the lowercase letter, and the namespace and a hyphen go in front:
| Folder name | Import specifier | Markup tag |
|---|---|---|
| `greetingCard` | `c/greetingCard` | `<c-greeting-card>` |
| `accountSummaryPanel` | `c/accountSummaryPanel` | `<c-account-summary-panel>` |
| `map` | `c/map` | `<c-map>` |
| `section2Header` | `c/section2Header` | `<c-section2-header>` |
Digits do not trigger a hyphen. Only capital letters do. A tag must always have a closing tag. Self-closing syntax like <c-greeting-card /> is not valid in LWC templates:
<template>
<c-greeting-card name="Ada"></c-greeting-card>
</template>Inside Aura markup, the same component is written <c:greetingCard>, with a colon and the camelCase name. The folder name is the same. Only the tag syntax differs.
Checking names in code
You can express the folder rules and the tag conversion as a small script. This is useful as a pre-commit check in a team that creates many components. Save it as check-names.js and run it with Node.js.
const FOLDER_RULE = /^[a-z](?:[a-zA-Z0-9]|_(?=[a-zA-Z0-9]))*$/;
function isValidFolderName(name) {
return FOLDER_RULE.test(name);
}
function toTag(name, namespace = 'c') {
const kebab = name.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase());
return `${namespace}-${kebab}`;
}
const samples = [
'greetingCard',
'accountSummaryPanel',
'Greeting',
'greeting-card',
'greeting__card',
'greeting_',
'my_card'
];
for (const name of samples) {
const ok = isValidFolderName(name);
console.log(`${name.padEnd(22)} ${ok ? 'valid ' : 'invalid'} ${ok ? toTag(name) : ''}`);
}The regular expression requires a lowercase first letter. After that, each character is a letter, a digit, or an underscore that is followed by a letter or digit. That last condition rules out both a trailing underscore and a double underscore. It does not check uniqueness, which needs a look at the whole namespace.
Rules for the files
Each file with a defined role follows the pattern <folderName>.<extension>:
| Role | File name | Required? |
|---|---|---|
| Template | `greetingCard.html` | Needed for any component that renders markup |
| Class | `greetingCard.js` | Yes |
| Styles | `greetingCard.css` | No |
| Configuration | `greetingCard.js-meta.xml` | Needed to deploy and to expose the component |
| Icon | `greetingCard.svg` | No |
Three details matter here.
Case must match exactly. greetingCard.html and GreetingCard.html are different names. On macOS and Windows the file system is usually case-insensitive, so a mismatch may work locally. It then fails on a Linux build server or when the platform compiles the bundle. Git can also hold two spellings of one file after a rename on a case-insensitive system. Treat the case as significant everywhere.
The configuration extension is .js-meta.xml. It is one extension, not .js followed by something else. A file named greetingCard.meta.xml or greetingCard-meta.xml is not recognized.
Extra files do not follow the rule. A bundle can also contain helper JavaScript modules, additional templates and CSS-only modules. These use their own names, such as utils.js, signedIn.html or sharedStyles.css. They are the exception, because they are not the component's main files. Later chapters cover each one. For now, remember that the folder-name rule applies to the main five files, not to every file in the bundle.
Generating a correctly named bundle
The Salesforce CLI creates the folder and files with the names already matched. Run it from the project root:
sf lightning generate component --type lwc --name greetingCard --output-dir force-app/main/default/lwcThis creates greetingCard.html, greetingCard.js and greetingCard.js-meta.xml inside lwc/greetingCard. The CLI creates only the files it needs. It does not create the CSS file, the SVG or the tests, so you add those yourself, using the same base name.
The VS Code Salesforce extensions offer the same action as "SFDX: Create Lightning Web Component". Using a generator is the safest way to avoid typos in the base name.
Common mistakes
- Renaming the folder but not the files. A bundle in
greetingPanel/that still containsgreetingCard.jswill not compile. Rename the folder and every main file together. - Using a hyphen to mirror the tag. A folder named
greeting-cardis invalid. Name itgreetingCardand use the generated tagc-greeting-card. - Writing the tag in camelCase.
<c-greetingCard>does not resolve, because markup tags in LWC templates are lowercase kebab-case. - Starting the name with a capital letter.
GreetingCardis a valid class name inside the file, but it is not a valid folder name. The class name inside the JavaScript file is free. Only the folder and file names carry the rules. - Creating a duplicate name. Two bundles named
greetingCardin the same namespace collide, even if they live in different package directories of the same project.
Exercise
A teammate creates the folder Order_Summary containing OrderSummary.html, OrderSummary.js and OrderSummary.js-meta.xml. Name every problem, then give the corrected folder name, the import specifier and the markup tag.
Show answer
The folder name starts with a capital letter, which breaks the lowercase-first rule. Underscores are allowed in the middle of a name, but the convention is camelCase, so the underscore should go. The file base names (OrderSummary) do not match the folder name (Order_Summary), so the bundle will not resolve its files. The corrected folder is orderSummary, with files orderSummary.html, orderSummary.js and orderSummary.js-meta.xml. The import specifier is c/orderSummary and the markup tag is <c-order-summary></c-order-summary>.
Exercise
Using the rule that each capital letter becomes a hyphen plus its lowercase form, what is the tag for a component in the c namespace whose folder is kpiCard2Header? Is kpi__card a valid folder name?
Show answer
The tag is <c-kpi-card2-header>. The capital C and H each add a hyphen, and the digit 2 does not. kpi__card is not valid, because it contains two consecutive underscores.