Before writing TaskFlow's first component you need to prepare the work environment: install the basic tools, create a project with a sensible structure, and check that everything works by serving a page in the browser. This lesson is purely practical and is designed for someone starting from scratch: by the end you will have a project folder ready, with the lit package installed, a development server running, and the editor configured to work comfortably with Lit templates.
Contents
- Installing Node.js
- Creating the project with Vite (Lit template)
- Alternative:
npm init @open-wc - Installing the
litpackage - Recommended folder structure
- Serving the project locally
- Useful editor extensions
- JavaScript or TypeScript: this course's decision
- Installing Node.js
Lit itself is a library that runs in the browser and does not need any special environment to work. However, for the modern development workflow — managing dependencies, running a development server with automatic reload, bundling the project — Node.js is used together with its package manager, npm.
Steps:
- Download and install the LTS (Long Term Support) version of Node.js from nodejs.org. On Linux you can also use your distribution's package manager or a version manager like
nvm. - Check the installation by opening a terminal and running:
You should see two version numbers. For this course, any version of Node.js 18 or higher is enough, since that is the minimum required by the modern tools in the ecosystem (Vite, Lit 3.x).
- Creating the project with Vite (Lit template)
Vite is a development tool that is very popular for its almost instant startup and its hot reloading. It comes with official templates for several ecosystems, including Lit.
To create a new project called taskflow:
The wizard will create a taskflow folder with a sample project already configured to work with Lit. Next, go into the folder and install the dependencies:
And start the development server:
Vite will show a local URL (usually http://localhost:5173). When you open it in the browser you will see the sample component that comes with the template, with automatic reload every time you save a change to the code.
Note: the
litVite template uses TypeScript by default. There is also an explicitlit-tstemplate, and if you prefer plain JavaScript, you can start from thevanillatemplate and addlitmanually (this is explained in section 4). Later in this same lesson, we discuss how to choose between JavaScript and TypeScript to follow the course.
- Alternative:
npm init @open-wc
npm init @open-wcAnother route, maintained by the Open Web Components community (a set of tools and recommendations specifically designed for Web Components, not just for Lit), is the @open-wc wizard:
This wizard interactively asks what you want to generate (an application, a component publishable as a package...), whether you want to use TypeScript, and whether you want to include linting, testing or demo tools. It is a somewhat more complete option aimed at those who are going to publish reusable components as independent packages, while the Vite template is more direct for starting an application as quickly as possible.
For the purposes of this course, either route is valid; the examples will assume a project structure created with Vite, as it is the most widespread and the simplest to get started with.
- Installing the
lit package
lit packageIf you start from a blank template (for example, Vite's vanilla template) or you simply want to know what happens "under the hood," installing Lit manually is as simple as:
This adds lit to the project's package.json dependencies. From there, in any JavaScript or TypeScript file in the project you can import what you need:
No additional bundler configuration is needed: lit is distributed as standard ES modules, compatible with Vite and with any modern bundling tool.
- Recommended folder structure
A Lit project does not impose a specific folder structure — unlike some more opinionated frameworks — but it is worth adopting a clear organization from the start, especially because TaskFlow will grow with several components throughout the course. A recommended structure for this course is:
taskflow/
├── index.html
├── package.json
├── src/
│ ├── main.js # Application entry point
│ ├── components/
│ │ ├── task-card.js
│ │ ├── task-list.js
│ │ ├── task-board.js
│ │ ├── task-filter.js
│ │ └── user-avatar.js
│ └── styles/
│ └── shared-styles.js # Shared styles (module 4)
└── public/
└── favicon.svgKey ideas behind this organization:
- Each component lives in its own file, with a name that matches the name of the HTML tag it defines (
task-card.jsdefines<task-card>). - The
components/folder groups together all of the application's Web Components, separate from the startup logic (main.js). - As the course progresses, this structure will be expanded (for example, with a folder for reactive controllers or for tests), but the idea of "one component, one file" will be kept throughout the entire course.
- Serving the project locally
During development, Lit components run directly in the browser, so a server that just serves the project's static files is enough. We have already seen that Vite offers this with npm run dev, but it is worth understanding exactly what that command does:
- It starts a local HTTP server (there is no need to configure Apache, Nginx or anything similar).
- It serves
index.htmland resolves ES modules (import ... from 'lit') on the fly, with no need to generate abundlebeforehand. - It watches for changes in the project's files and automatically refreshes the browser (or even updates the specific module without reloading the whole page, thanks to "Hot Module Replacement").
To generate a version optimized for production (minified, with the modules bundled), Vite also offers:
The result is generated in a dist/ folder. The production bundling process, with more detail on optimization and publishing, is studied in the integration and deployment module; for now it is enough to know that this second command exists.
- Useful editor extensions
Working with Lit templates — blocks of HTML embedded inside JavaScript through the html function — is much more comfortable with proper editor support. If you use Visual Studio Code (the most common editor in the ecosystem), these are the recommended extensions:
| Extension | What it provides |
|---|---|
| lit-plugin | HTML syntax highlighting inside html\...`` templates, tag and attribute autocompletion, and type checking on template expressions. |
| ES6 String HTML | A lighter alternative focused only on HTML and CSS syntax highlighting inside tagged template strings. |
| Prettier | A code formatter that understands JavaScript/TypeScript and keeps a consistent style across the whole project. |
lit-plugin internally relies on a tool called lit-analyzer, which also exists as a command-line tool and can be integrated into continuous integration to catch errors in templates (for example, misspelled attributes) before reaching production. We will come back to code quality tools in the testing and best practices module.
- JavaScript or TypeScript: this course's decision
Lit is written in TypeScript and offers excellent integration with it (typed properties, template autocompletion, type-checked decorators). However, TypeScript is not required: Lit works just as well with plain JavaScript.
There are two common ways to declare a component class in Lit:
- With decorators (
@customElement,@property,@state...): this is the most concise syntax and the one the official documentation uses by default. Decorators are a JavaScript proposal that, as of today, requires a build tool that supports them (TypeScript, or Babel with the corresponding plugin in JavaScript projects). Vite already comes with this capability configured in its Lit template. - Without decorators, with
static properties: an alternative form, fully equivalent in outcome, designed for plain JavaScript projects that do not want to depend on a build step with decorator support. It is more verbose but requires no additional configuration.
This course will use JavaScript, showing both variants (decorators and static properties) wherever it makes sense to compare them, so you can choose the one that best fits your own projects. This choice will first come up in detail in the next lesson, when building the first real component.
Common Mistakes and Tips
- Using an old version of Node.js: if
npm create vite@latestfails with strange errors, the first thing to check is the installed Node.js version. Updating it usually solves the problem. - Forgetting
npm installafter cloning or creating the project: without this step, thelitpackage (and the rest of the dependencies) will not be available and the project will not start. - Mixing decorator syntax without the proper build configuration: if you copy examples with
@customElementinto a project that does not have decorator support configured, syntax errors will appear. The Vite template for Lit already comes ready for this; if you are starting from scratch, it is worth checking first. - Confusing the development server with a production server:
npm run devis not meant to serve real traffic; for production you first generate thebuildand serve the resulting static content.
Exercises
- Install Node.js if you don't have it yet, and check the
nodeandnpmversions from the terminal. - Create a new project called
taskflowusing Vite's Lit template, install the dependencies, and start it in development mode. Check that you can open it in the browser. - Inside the newly created project, create the
src/components/folder and an emptytask-card.jsfile inside it, following the folder structure recommended in this lesson.
Solutions
- The check is done with:
If both commands return a version number (for example v20.11.0 and 10.2.4), the installation is correct.
After the last command, the terminal will show a local URL (for example http://localhost:5173); opening it in the browser should show the Lit template's sample page.
On Windows, without touch available by default, it is enough to create the empty file from the code editor, saving it at that path.
Conclusion
In this lesson you have prepared all the groundwork needed to start programming: Node.js installed, a TaskFlow project created with Vite (or alternatively with @open-wc), the lit package available, a folder structure designed to grow, and an editor configured with specific support for Lit templates. You have also seen that this course will use JavaScript, alternating between decorator syntax and static properties as appropriate.
With the environment ready, in the next lesson you will stop configuring and start programming: you will create your first real Lit component and, with it, the first — fully static — version of <task-card>, the fundamental piece of TaskFlow.
Lit Course
Module 1: Introduction to Lit and Web Components
- What are Web Components and why Lit?
- Setting Up the Development Environment
- Your First Lit Component
- Anatomy of a Lit Component
Module 2: Reactive Templates and Rendering
- Lit's Template Engine
- Expressions and Interpolation in Templates
- Conditional Rendering
- List Rendering
- The Rendering Cycle
Module 3: Reactive Properties and State
- Reactive Properties
- Internal State with @state
- Types of Properties and Custom Converters
- Attributes vs Properties and Reflection
Module 4: Styling Lit Components
- Encapsulated CSS with Shadow DOM
- Shared Styles Between Components
- Custom CSS Properties and Theming
- Slots and Styling Distributed Content
Module 5: Events and Component Communication
- Handling DOM Events in Templates
- Custom Events: Communication from Child to Parent
- Communication from Parent to Child with Properties
- Communication Patterns Between Sibling Components
Module 6: Lifecycle and Advanced Behavior
- Lifecycle Callbacks
- Reactive Hooks: willUpdate, updated, and firstUpdated
- Reactive Controllers
- Mixins and Composing Behavior
Module 7: Directives and Advanced Template Features
- Built-in Directives: classMap, styleMap and ifDefined
- Custom Directives
- Asynchronous Rendering with until
- Shared Context with @lit/context
Module 8: Integration, Interoperability and Deployment
- Using Lit Components in Plain HTML
- Integrating Lit with React, Vue, and Angular
- Server-Side Rendering with @lit-labs/ssr
- Bundling, Publishing, and TypeScript
Module 9: Testing and Best Practices
- Unit Tests with Web Test Runner
- Accessibility in Web Components
- Performance and Optimization
- Common Patterns and Anti-patterns
