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


Designing Fun and Engaging Levels for My Space-Themed Match-3 Game
8/2/2025

Designing levels for my space-themed match-3 game has been a journey of balancing **fun, challenge, and variety**. In this article, I share my experience with **creating engaging puzzles, managing difficulty, and keeping gameplay fresh**. From playtesting to strategic layouts, these insights shape my approach to making levels that players will love. 🚀

From Burnout to Balance: Rediscovering Energy in Game Dev, Fitness, and Life
7/2/2025

A reflection on how burnout, illness, and lost momentum forced me to rethink my approach to productivity, motivation, and balance. Now, I’m refactoring my life—one habit at a time—to rebuild my energy without falling into the same cycle again.

New Year, New Code: My Developer Resolutions for the Year
29/12/2024

In this blog post, I share my New Year's resolutions as a developer: posting more about my work on social media, writing more articles, and finally finishing and publishing a game. Plus, I offer practical tips on setting realistic and achievable goals to help fellow developers kick off the year with purpose!

Bad Practices in JavaScript: Common Pitfalls and How to Avoid Them 🚫
28/12/2024

JavaScript is like the Swiss Army knife of web development—it’s everywhere, from tiny website features to massive, complex web apps. But with great power comes... well, the chance to make a mess! Its flexibility and loose rules make it super easy to use, but also super easy to misuse.