How to Apply Changes Directly in NodeJS Application without having to restart it using nodemon

Posted on

Introduction

This article is showing how to use a specific module in NodeJS application. Basically, the main purpose is to get the changes in a NodeJS application directly. So, the change will automatically apply to the running NodeJS application. The module for achieving the purpose is ‘nodemon’. So, in order to achieve it, just install the nodemon module. For installing nodemon, just execute the command for installing module. Reference for installing NodeJS module exist in this link. That article has a title of ‘NodeJS Article’.

 

Steps for Applying Changes Directly in NodeJS without restarting using Nodemon

This is a sequence for applying changes directly in the execution of NodeJS application. The sequence consist of several parts. The first part is the preparation. The following is the continuation of the preparation. It is the execution of the NodeJS applcation. The first part is the preparation. So, the following are the steps to prepare it :

Preparation Part

Basically, the preparation part is just an access to the server, npm installation and also nodemon module installation.

  1. Access the server or the machine. Directly access or remotely access is fine. Remotely access is possible with SSH method. The reference exist in this link. It is an article with the title of ‘How to Remote CentOS Virtual Server running in a VirtualBox with a NAT Network using SSH’.

  2. First of all, make sure that npm tool is available in the operating system. Read the article for installing npm if it is not available. It exist in this link as a reference. It is an article with the title of ‘How to install npm’. Additionally, further reference also exist in this link. It is an article with the title of ‘How to Install npm in Ubuntu Linux operating system’. All of it exist in the article with the title of ‘NodeJS Article’ in this link. Just type ‘npm’ in the command line to make sure it is available.

  3. Soon after, do not forget to install the nodemon module. Specifically, the article for installing nodemon is available with the title of ‘How to Install Nodemon Module in NodeJS Application’ in this link.

Execution Part

After finishing the preparation part, this is the actual part. It is a part where the execution of the NodeJS application exist.

  1. The first step, create a NodeJS application using just one script. For an example, the script name is app.js. Just put any content on it and then test it. The following is a simple content of it. The first content exist as follows :

    console.debug("Testing NodeJS application...")
    
  2. Next, instead of running the script using the ‘node’ command, just use the nodemon command as follows :

    [admin@10 test]$ nodemon app.js
    [nodemon] 2.0.7
    [nodemon] to restart at any time, enter `rs`
    [nodemon] watching path(s): *.*
    [nodemon] watching extensions: js,mjs,json
    [nodemon] starting `node app.js`
    Testing NodeJS application...
    
  3. Following after, edit the script with the name of app.js exist in the previous step. The content exist below :

    cons express = require("express")
    console.debug("Testing NodeJS application...")
    
  4. Apparently, the output of the above editing process will trigger nodemon processing it further. It will simultaneously produce the following output. The output appear without having to execute the nodemon command as in the previous step :

    [nodemon] clean exit - waiting for changes before restart
    [nodemon] restarting due to changes...
    [nodemon] starting `node app.js`
    [nodemon] restarting due to changes...
    [nodemon] starting `node app.js`
    Testing NodeJS application...
    [nodemon] clean exit - waiting for changes before restart
    [nodemon] restarting due to changes...
    [nodemon] starting `node app.js`
    /home/admin/nodejs/test/app.js:1
    cons express = require("express")
         ^^^^^^^
    SyntaxError: Unexpected identifier
        at wrapSafe (internal/modules/cjs/loader.js:979:16)
        at Module._compile (internal/modules/cjs/loader.js:1027:27)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
        at Module.load (internal/modules/cjs/loader.js:928:32)
        at Function.Module._load (internal/modules/cjs/loader.js:769:14)
        at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
        at internal/main/run_main_module.js:17:47
    [nodemon] app crashed - waiting for file changes before starting...
    
  5. So, in order to correct the error message above, just correct the source file. As the correction is made, nodemon will process it in realtime. In an instance, the following is the correction of the app.js file content :

    const express = require("express")
    console.debug("Testing NodeJS application...")
    
  6. Moreover, editing the app.js file is just changing just from ‘cons’ to ‘const’ as exist above. The purpose is for declaring a constant. Unfortunately, it still trigger an error as follows :

    [nodemon] restarting due to changes...
    [nodemon] starting `node app.js`
    internal/modules/cjs/loader.js:883
      throw err;
      ^
    Error: Cannot find module 'express'
    Require stack:
    - /home/admin/nodejs/test/app.js
        at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
        at Function.Module._load (internal/modules/cjs/loader.js:725:27)
        at Module.require (internal/modules/cjs/loader.js:952:19)
        at require (internal/modules/cjs/helpers.js:88:18)
        at Object. (/home/admin/nodejs/test/app.js:1:17)
        at Module._compile (internal/modules/cjs/loader.js:1063:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
        at Module.load (internal/modules/cjs/loader.js:928:32)
        at Function.Module._load (internal/modules/cjs/loader.js:769:14)
        at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
      code: 'MODULE_NOT_FOUND',
      requireStack: [ '/home/admin/nodejs/test/app.js' ]
    }
    [nodemon] app crashed - waiting for file changes before starting...
    
    [nodemon] restarting due to changes...
    [nodemon] starting `node app.js`
    [nodemon] restarting due to changes...
    [nodemon] starting `node app.js`
    Testing NodeJS application...
    [nodemon] clean exit - waiting for changes before restart
    
  7. Finally, solve the above error with additional solution. Since the main message is because of the module with the name of ‘express’ is not available, just install it. For further reference, the solution is installing the ‘express’ module. Actually, the installation of the ‘express’ module in NodeJS application available in this link.

Leave a Reply