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

14 Jul, 2020
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:
Create a file index.js with the above code.Run the file on Terminal with the following command:node index.jsOutput:Output of program in Node.js
Create a file index.js with the above code.
Run the file on Terminal with the following command:node index.jsOutput:Output of program in Node.js
node index.js
Output:
Output of program in Node.js
Steps to Run the code in Browser:
Create a file index.js with the above code.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>To run the code, double click on index.html and it will open in browser.Open browser console to check the output.Output:Output of program in Browser
Create a file index.js with the above code.
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>
<!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>
To run the code, double click on index.html and it will open in browser.
Open browser console to check the output.Output:Output of program in Browser
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.
Node.js-Misc
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between dependencies, devDependencies and peerDependencies
Mongoose Populate() Method
Node.js Export Module
Mongoose find() Function
How to connect Node.js with React.js ?
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript