How to create Express Js application in a minute

Now, you do not need to create each file and directory for your ExpressJs project structure. You just need to run a command and everything will be created for you.

So, Let’s start with step by step.

1. Before you start, you must create an directory for your Express app.

mkdir express-example

2. Now, go to your newly created directory .

cd express-example

3. Inside this directory, just run this command.

npx express-generator

This command would give you output something just like this.

express-generator output

4. Great! Your Express app skeleton has been created. Now, it’s time to open your project directory in your preferred IDE, such as Visual Studio Code or any other IDE of your choice. In this case, the project directory will be named “express-example”.

5. Here, you will notice some high and critical warnings.

express warnings

To get rid off these warnings, just update “express” version to the newest and replace jade with pug.

Old package.json

{
  "name": "express-example",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "jade": "~1.11.0",
    "morgan": "~1.9.1"
  }
}

New package.json

{
  "name": "express-example",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "express": "~4.18.2",
    "http-errors": "~1.6.3",
    "pug": "3.0.2",
    "morgan": "~1.9.1"
  }
}

You can see, inside the new package.json, I’ve updated the express version to ~4.18.2 which is the newest version when i am writing this blog. With that, jade is replaced with pug. Now, you need to run this command again.

npm install

After that, All the warnings would be disappear.

6. Additionally, it is important to replace “jade” with “pug” in the app.js file, as well as renaming the files within the views directory to have the .pug extension.

7. Now run “npm start” in the root directory and access it on http://localhost:3000.

Leave a Reply