Open In App

How to check whether a script is running under Node.js or not ?

Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, there is not any specific function or method to get the environment on which the script is running. But we can make some checks to identify whether a script is running on Node.js or in the browser.

Using process class in Node.js: Each Node.js process has a set of built-in functionality, accessible through the global process module. The process module doesn’t need to be required – it is somewhat literally a wrapper around the currently executing process, and many of the methods it exposes are actually wrappers around calls into core C libraries.

Code Snippet:




if ((typeof process !== 'undefined') && 
(process.release.name.search(/node|io.js/) !== -1)) {
    console.log('this script is running in Node.js');
} else {
    console.log('this script is not running in Node.js');
}


In this code snippet, all we are doing is just checking if the process module is exists or not. If the process is not undefined then we have to check if the name property of release method in process class is either node or io.js (for io.js releases of the node).

Using module in Node.js: This is the generally accepted solution that is also used in the underscore.js library. This technique is actually perfectly fine for the server-side, as when the require function is called, it resets the this object to an empty object, and redefines module for you again, this means you don’t have to worry about any outside tampering. As long as your code is loaded with require, you are safe.

Code Snippet:




if (typeof module !== 'undefined' && module.exports) {
    console.log('this script is running in Node.js');
} else {
    console.log('this script is not running in Node.js');
}


However, this falls apart on the browser, as anyone can easily define a module to make it seem like it’s the object you are looking for. On one hand, this might be the behavior you want, but it also dictates what variables the library user can use in the global scope. Maybe someone wants to use a variable with the named module that has exports inside of it for another use.

Steps to Run the code with Node.js:

  1. Create a file index.js with the above code.
  2. Run the file on Terminal with the following command:
    node index.js

    Output:

    Output of program in Node.js

Steps to Run the code in Browser:

  1. Create a file index.js with the above code.
  2. Create another file index.html in the same directory with the code below:




    <!DOCTYPE html>
    <html>
    <head>
        <title>GFG</title>
        <script src="./index.js"></script>
    </head>
    <body>
        <h3>
            How to check whether a script is 
            running under Node.js or not?
        </h3>
    </body>
    </html>

    
    

  3. To run the code, double click on index.html and it will open in browser.
  4. Open browser console to check the output.

    Output:


    Output of program in Browser

Conclusion: You can use any of the above methods to determine whether your script is running in Node or not. But the problem with trying to figure out what environment your code is running is that any object can be modified and re-declared making it close to impossible to figure out which objects are native to the environment, and which have been modified by the program.



Last Updated : 14 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads