Task Runners and Scaffolding
Level 5 · Building the project
How npm scripts give each job a short name. How one command can write all the first files of a new project for you.
Course contentsLesson 13 of 13
Level 1 · The editor
Level 2 · Checking and fixing code
Level 3 · Trying code out
Level 4 · Other people's code
Level 5 · Building the project
After this lesson you can
- Run a project's jobs with npm run
- Add a task to package.json
- Start a new project from a scaffold
11 mincovers 3 glossary entries
Worth reading first: Make, Maven and Gradle
How a project names its jobs and starts its first files
A task runner runs a project's jobs by short names, such as build, test or dev. In a JavaScript project the tasks live in the scripts part of package.json, and npm is the runner. npm run build runs the longer real command behind it. A new Vite project starts with 4 of them: dev, build, lint and preview.
Each task is just a name and a command. The name dev might run a local server, build might pack the site for release, and test might run the tests. npm test and npm start are so common that they work without the word run.
The point is that nobody has to remember the real commands. They sit in the project, next to the code, and they change with it. A new teammate reads the scripts and knows how to work the project in a minute.
Scaffolding writes a new project's first files for you. You run one command and answer a few questions, such as the name and the framework. It then writes the folders, the settings and a small page that already runs. You start from working code, not an empty folder.
A common one for web projects is npm create vite@latest. Vite is a fast build tool and local server for web pages. Its scaffold asks for a name and a framework. Then it writes a folder with a project file, a settings file and a starter page.
After the scaffold, three commands get you to a page that runs. Go into the folder, install the packages, and run the dev task. The page then reloads each time you save a file.
The common mistake is keeping each starter file without reading it. You end up with settings you do not know and sample code that ships by mistake. Read each file once, and delete what you do not need.
"scripts": {
"dev": "vite",
"build": "vite build",
"test": "vitest"
}$ npm create vite@latest my-app -- --template react
$ cd my-app
$ npm install
$ npm run devWorked example
Lucy joins a new team
On her first day, Lucy opens the project file and finds 6 scripts. They are dev, build, test, lint, format and preview.
She runs npm install, then npm run dev, and the site is up on her laptop in 3 minutes. Nobody had to send her a list of commands.
The words for this lesson3
Check yourself
Where do npm run tasks come from?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.