How to check whether a script is running under Node.js or not ?
Last Updated :
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:
javascript
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:
javascript
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.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:
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
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.
Similar Reads
How to check any script is running in linux using Python? Python is a strong and exponentially growing programming language in the present day. There is a multiple-way to check which script is running in the background of a Linux environment. One of them is using the subprocess module in python. Subprocess is used to run new programs through Python code by
2 min read
How to Change npm start Script of Node.js ? In Node.js, npm (Node Package Manager) provides a convenient way to manage project scripts through the scripts field in the package.json file. By default, the npm start command is used to start your Node.js application. However, you might need to customize this command to suit specific requirements,
3 min read
Check if Node.js MySQL Server is Active or Not To check if a MySQL server is active or not from a Node.js application, you typically want to establish a connection to the MySQL server and handle any errors that occur during the connection attempt. Hereâs a step-by-step guide on how to achieve this.Prerequisites:NodeJSExpressJSMySQLApproachTo che
2 min read
How to Detect AJAX Request to Normal Request in Node.js ? When developing web applications with Node.js, you often need to differentiate between AJAX (Asynchronous JavaScript and XML) requests and normal HTTP requests. AJAX requests are typically used to fetch data without reloading the entire page, while normal requests often result in full page loads. De
3 min read
How to check for undefined property in EJS for Node.js ? Handling undefined attributes is essential when working with EJS templates in a NodeJS application to guarantee a seamless and error-free user experience. In this post, we'll examine one of the most basic methods for determining whether a variable is defined or not, which involves utilizing a simple
3 min read
How to print command line arguments passed to the script in Node.js ? Node.js is an open-source and cross-platform runtime environment built on Chrome's V8 engine that enables us to use JavaScript outside the browser. Node.js helps us to use build server-side applications using JavaScript. In Node.js if you want to print the command line arguments then we can access
2 min read
How to Get the Path of Current Script using Node.js ? In Node JS, getting the path of the current script is useful for file operations, logging, and configuration. It allows you to access related files or directories reliably, regardless of where your Node.js process was started.ApproachTo get the path of the present script in node.js we will be using
2 min read
How to check if a jQuery plugin is loaded? There are multiple ways by which we can simply check whether the jQuery plugins are loaded successfully or not using jQuery. We can also check whether a particular function within the plugin is accessible or not. This tutorial will demonstrate how to check if a jQuery plugin is loaded or not. Step 1
3 min read
How to check a function is defined in JavaScript ? In this article, we will check whether a function is defined or not in JavaScript. The JavaScript typeof operator is used to check whether the function is defined or not. JavaScript typeof OperatorThe typeof operator is used to find the type of a JavaScript variable. This operator returns the type o
2 min read
How to Run, Configure, and Troubleshoot npm Scripts? npm (Node Package Manager) is not only used for managing dependencies in Node.js projects but also provides a powerful script-running functionality. npm scripts allow you to automate various tasks such as running tests, building your project, deploying applications, and more. This guide will walk yo
5 min read