Using Express Generator

1. Installing express-generator

CommandPurpose
npm install -g express-generatorGlobal install
npx express-generatorOne-off without install RECOMMENDED
express --helpList options

2. Creating New Projects

Example: Scaffold project

npx express-generator my-app --view=ejs --git
cd my-app
npm install
npm start  # runs DEBUG=my-app:* node ./bin/www
FlagEffect
--view <engine>ejs, pug, hbs, hjs, jade, twig, vash
--no-viewSkip view engine (API-only)
--css <preprocessor>less, stylus, compass, sass
--gitAdd .gitignore
-f, --forceForce on non-empty directory

3. Specifying View Engine

EngineSyntax Style
EJSHTML with <% %> tags
PugIndentation-based, terse
Handlebars (hbs)Logic-less {{var}}
TwigSymfony-style

4. Generating with CSS Preprocessors

Example: SASS project

npx express-generator --view=pug --css=sass my-styled-app
PreprocessorGenerated middleware
sassnode-sass-middleware
lessless-middleware
stylusstylus middleware

5. Adding Git Ignore Files

Example: Generated .gitignore

node_modules/
*.log
.env
.DS_Store
FlagResult
--gitAdds standard .gitignore

6. Understanding Generated Structure

my-app/
├── bin/
│   └── www              ← server bootstrap (port + listen)
├── public/              ← static assets
│   ├── images/
│   ├── javascripts/
│   └── stylesheets/
├── routes/
│   ├── index.js
│   └── users.js
├── views/
│   ├── error.ejs
│   ├── index.ejs
│   └── layout.ejs
├── app.js               ← Express app definition
└── package.json
        
FileRole
bin/wwwCreates HTTP server, normalizes port, binds app.listen()
app.jsApplication + middleware + route mounting
routes/One express.Router() per resource

7. Installing Dependencies

Example: Install + audit

cd my-app
npm install
npm audit fix
npm outdated
CommandPurpose
npm ciClean reproducible install (CI/CD)
npm install --omit=devProduction install (no devDeps)

8. Starting Generated Application

CommandBehavior
npm startnode ./bin/www
DEBUG=my-app:* npm startVerbose debug output
npx nodemon ./bin/wwwAuto-restart on changes
node --watch ./bin/wwwNative watch mode NODE 18.11+

9. Customizing Generated Templates

CustomizationWhere
Add middlewareapp.js (between body parsers and routes)
Add routesNew file in routes/, mount in app.js
Change layoutviews/layout.ejs
Replace view engineUpdate app.set("view engine") + reinstall

10. Using npx for One-Time Execution

Example: Quick prototype

npx express-generator --no-view --git --force ./
npm install
node ./bin/www
BenefitDetail
Always latestPulls from npm cache
No global pollutionNothing installed permanently
Version pinningnpx express-generator@4.16.1
Note: express-generator targets Express 4. For Express 5+ projects, hand-craft the structure or use a modern starter (e.g. Fastify, Hono) for native ESM + TypeScript.