Enhancing Our Node.js API: Hot Reloading with Nodemon, Bundling with Webpack & Using ES6

Enhancing Our Node.js API: Hot Reloading with Nodemon, Bundling with Webpack & Using ES6

Created by eneaslari 20/9/2023

nodejs

Previous article: How to Make API using NodeJS and Express

1. Hot Reloading with Nodemon

Nodemon watches for changes in your source code and automatically restarts your server, improving your development experience.

Step 1: Install Nodemon:

npm install --save-dev nodemon

Step 2: Modify your package.json:

Add a start script:

"scripts": {
  "start": "nodemon app.js"
}

You can now use npm start to run your server with hot reloading.

2. Bundling Server Code with Webpack & Using ES6

To use ES6 and beyond features in Node.js, and also to bundle our server code, we can set up Webpack with Babel.

Step 1: Install required packages:

npm install --save-dev webpack webpack-cli @babel/core @babel/preset-env @babel/node babel-loader

Step 2: Webpack Configuration (webpack.config.js):

Create a webpack.config.js at your project root:

const path = require('path');

module.exports = {
    entry: './app.js',
    target: 'node',
    mode: 'development',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }
            }
        ]
    }
};

Step 3: Babel Configuration:

Create a .babelrc file in your project root:

{
  "presets": ["@babel/preset-env"]
}

Step 4: Modify the Nodemon start script:

In package.json, update the start script to use the bundled code:

"scripts": {
  "start": "nodemon --exec babel-node ./dist/bundle.js"
}

Step 5: Bundle the code:

Run the following command:

npx webpack

This will bundle your server code into dist/bundle.js.

3. Using ES6 Features

With Babel set up, you can now use ES6 features in your Node.js code. Here are some ways to enhance our existing code:

  1. Use Import/Export Syntax:

Instead of:

const express = require('express');

Use:

import express from 'express';

Similarly, change your module.exports to:

export default yourModuleName;

And then import it using:

import yourModuleName from './yourModulePath';
  1. Utilize Arrow Functions, Async/Await, and other ES6+ Features:

For instance:

exports.createUser = async (req, res) => {
    // Your code here
};

With the above configurations and changes, you've transformed your Node.js and Express API to use the latest ES6 features, bundled your server code, and added hot reloading capabilities. Happy coding!

More to read


My New Plan: Making a Mini-Game Every Week
21/2/2025

This week, I realized that working on big projects can sometimes feel slow, and it’s easy to lose motivation. So, I came up with a new plan—I’ll create and release a small game every week!

Making Tic-Tac-Toe and Sudoku in Just a Few Hours
20/2/2025

This week, I decided to take a break from my main game project and create something simple: **a Tic-Tac-Toe and a Sudoku game**.

How I Made My Game Development Workflow Stress-Free
19/2/2025

Game development can be overwhelming, especially when working on multiple tasks without a clear plan. For a while, I was struggling to organize my thoughts and decide what to focus on next

Designing Fun and Engaging Game Levels
18/2/2025

Creating levels for a game sounds simple, but it comes with its own set of challenges.