How to Use Express Generator for Quick Node.js Apps
Use the
express-generator by installing it globally with npm install -g express-generator, then run express myapp to create a new app folder. Navigate into the folder, install dependencies with npm install, and start the server using npm start.Syntax
The basic syntax to use Express Generator is:
express [options] [dir]- creates a new Express app in the specified directory.npm install -g express-generator- installs the generator globally.npm install- installs dependencies inside the created app folder.npm start- starts the Express server.
Options include --view to choose a template engine like pug, ejs, or handlebars, and --git to add a .gitignore file.
bash
npm install -g express-generator express [options] [dir] cd [dir] npm install npm start
Example
This example shows how to create a new Express app named myapp with the default settings, install dependencies, and start the server.
bash
npm install -g express-generator express myapp cd myapp npm install npm start
Output
> myapp@0.0.0 start
> node ./bin/www
Listening on port 3000
Common Pitfalls
Common mistakes include:
- Not installing
express-generatorglobally before runningexpress. - Forgetting to run
npm installinside the generated app folder, causing missing dependencies. - Trying to start the app without navigating into the app directory.
- Not specifying a view engine if you want one different from the default.
Always check that Node.js and npm are installed and updated before using the generator.
bash
/* Wrong: Trying to run express without global install */ express myapp /* Right: Install globally first */ npm install -g express-generator express myapp
Quick Reference
Summary tips for using Express Generator:
- Install globally:
npm install -g express-generator - Create app:
express myapp --view=pug --git - Install dependencies:
npm install - Start server:
npm start - Default port is 3000; open
http://localhost:3000in browser
Key Takeaways
Install express-generator globally before using it to create apps.
Run npm install inside the generated app folder to install dependencies.
Use npm start to launch the Express server on port 3000 by default.
Specify a view engine with --view option if you want templates.
Always navigate into the app directory before running npm commands.