Sub topic 10
Helper JavaScript modules
This chapter assumes you know ES module syntax (import and export) and the basic bundle layout from earlier chapters: one folder, with an HTML file, a JavaScript class and a configuration file that share the folder's name.
What a helper module is
A helper module is an extra .js file inside a component bundle. It holds plain functions or constants, so the main class stays short. A helper is not a component. It has no template and does not extend LightningElement.
Use a helper when logic does not depend on component state. Typical cases are formatting, validation and calculation.
force-app/main/default/lwc/
└── greetingCard/
├── greetingCard.html
├── greetingCard.js
├── greetingCard.js-meta.xml
└── formatName.jsNaming
The main class file must match the folder name. A helper has no such constraint, so you can choose any valid file name. It still has to be a .js file in the bundle folder. Pick a name that describes the job, such as formatName.js or validators.js.
Writing and importing a helper
Export functions with export. Import them in the main class with a relative path. Omit the .js extension.
// formatName.js
export function formatName(name) {
const trimmed = (name || '').trim();
if (!trimmed) {
return 'World';
}
return trimmed.charAt(0).toUpperCase() + trimmed.slice(1);
}// greetingCard.js
import { LightningElement, api } from 'lwc';
import { formatName } from './formatName';
export default class GreetingCard extends LightningElement {
@api name = 'World';
get displayName() {
return formatName(this.name);
}
}The template then binds to the getter with {displayName}. The helper does the work, and the class decides when to call it.
<template>
<lightning-card title="Greeting">
<p class="greeting">Hello, {displayName}!</p>
</lightning-card>
</template>Sharing code across components
A helper file belongs to its own bundle. Other components should not import it by reaching into your folder. To share code, create a separate bundle that contains only a JavaScript file. Give it the same name as its folder, and export the shared functions from it. Then import it by module name.
// lwc/stringUtils/stringUtils.js
export function formatName(name) {
const trimmed = (name || '').trim();
return trimmed ? trimmed.charAt(0).toUpperCase() + trimmed.slice(1) : 'World';
}// In any other component
import { formatName } from 'c/stringUtils';This follows the same idea as CSS-only modules. A bundle without a template can still be a module that others import.
Common errors
- Wrong import path.
import { formatName } from 'formatName'is read as a module name, not a file. Use./formatNamefor a file in the same folder. - Using
thisin a helper. A plain exported function has no access to the component. Pass values in as arguments and return a result. - Adding
@apito a helper. Decorators like@apionly work in a class that extendsLightningElement. - Testing through the DOM only. Helpers are plain functions, so you can test them directly with Jest by importing them, with no
createElementcall.
Exercise
A component orderSummary needs a formatCurrency function. A second component, invoiceList, needs the same function. Where should the function live, and how does each component import it?
Show answer
Put it in a separate bundle that contains only a JavaScript file, for example lwc/currencyUtils/currencyUtils.js, and export formatCurrency from it. Both components import it with import { formatCurrency } from 'c/currencyUtils';. A helper file inside orderSummary would be imported with ./formatCurrency, but invoiceList could not use it without a duplicate copy.