src folder
Source modules for the application.
A JavaScript project needs a file structure and package setup that can grow without turning into a folder of random scripts.
src/ app.js api/items.js ui/render-items.js package.json
Modules & Code Organization
Small projects can start with one script. Real projects need clear folders, entry points and shared helpers.
npm manages project metadata, scripts and dependencies through package.json.
Good structure makes it obvious where new code should go and which files are public entry points.
Source modules for the application.
The file loaded by HTML or the build tool.
Project metadata, scripts and dependencies.
Repeatable commands for development and build tasks.
Examples
src/
api/
ui/
shared/
"scripts": { "dev": "vite", "build": "vite build" }
script.js new-script.js final-script-v3.js helpers-copy.js
Code patterns
These examples focus on imports, exports, module boundaries, on-demand loading and file structure you can use in real projects.
Group related code by feature.
src/features/search/ search-api.js search-view.js search-state.js
Keep generic helpers separate.
src/shared/format-date.js src/shared/dom.js
Name repeatable commands.
{
"scripts": {
"dev": "vite",
"build": "vite build"
}
}
Import from package names after installing.
import { marked } from "marked";
Rules that matter
A module system only helps when files have clear jobs, small public APIs and predictable import paths.
Make startup easy to find.
API, UI and shared logic should be recognizable.
Commands should be repeatable by the whole team.
They keep dependency installs reproducible.
Names should explain purpose.
A README should tell developers how to run the project.
Production thinking
File structure is architecture. It decides whether a project stays pleasant after the first week.
Shared UI and form helpers make accessible behavior easier to repeat correctly.
Production projects need reliable installs, repeatable builds and clear ownership of source files.
Good build structure helps ship stable assets, correct bundles and predictable page behavior.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain what a file exports, what it imports and why it exists, the module structure is doing its job.
Senior audit upgrade
Chapter checkpoint
Finish this chapter by turning the lessons into a small practical proof.
Build a small example that combines three lessons from this chapter.
Can you explain the important tradeoff without reading from the page?