For enquiries call:

Phone

+1-469-442-0620

Easter Sale-mobile

HomeBlogWeb DevelopmentHow to Run Node.js on Windows, macOS, Linux?

How to Run Node.js on Windows, macOS, Linux?

Published
05th Sep, 2023
Views
view count loader
Read it in
15 Mins
In this article
    How to Run Node.js on Windows, macOS, Linux?

    If you're going to use the Node.js environment for the first time in back-end JavaScript development, you need the right guidance to run it on your computer. We've curated a step-by-step guide on how to run Node.js on Windows, macOS, and Linux, along with other necessary details. Have a look. 

    36.19% of the skilled developers are using Node.js today for server-side scripting and developing responsive websites that run user requests on the web server and not on the client side or web browser. This not only reduces the loading time of any website by 50-60% but also increases the overall performance of all types of applications by 50%. 

    That's why global companies like Amazon, eBay, Netflix, Reddit, Tumblr, LinkedIn, PayPal, etc., use Node.js, and the number is increasing every day. Hence, you can understand the growing demand for a full-stack JavaScript developer; learning the Node.js program is the first step toward becoming one. Let's get started! 

    What is Node.js?

    Node.js is an open-source JavaScript runtime environment designed on Chrome’s V8 JavaScript engine. It renders cross-platform support to help the developers test and debug their code within a virtual domain irrespective of their operating system. Thus, developers can now perform server-side and client-side scripting using the same language, JavaScript. 

    Front-end developers use JavaScript to build interactive website UI, but that only works on the client-side or web browser. Node.js brings all the capabilities to JavaScript required for using it on back-end development, the server-side part of a web app architecture, and the serverless one. 

    How Does Node.js Work?

    Node.js works on a 'single-threaded event loop' model. That means it can respond to concurrent requests from a single thread while dismissing the requirements of blocking it for one client request only. Whenever this framework receives an incoming request, it is placed in the event queue first, and then the processing starts. Now two instances can happen. 

    If the client request doesn't require any blocking I/O (Input/Output), it will be processed instantly, and the response will be sent back to the source. 

    When the request needs some blocking I/O operations, i.e., accessing the database or communicating with the external services, it first picks a thread from the internal thread pool and assigns the request to that thread. Now that particular thread will first process the request by performing the required blocking I/O operations. Then, it will prepare the response and send it back to the event loop to send it to the client finally. 

    Thus, Node.js can serve multiple client requests parallelly by placing them in the event queue and getting each of them processed through the event loop. That’s why you experience faster performance while working in this lightweight environment than using any multi-threaded request-response architecture like HTML, ASP.NET, Ajax, etc.  

    Node.js comes with a package manager, npm. It works as an index of the libraries created by the Node.js developers' community. These packages are easily shared, modified, and imported by other projects. Yes, the npm registry hosts over 1,000,000 open-source packages that provide optimal solutions that make developing new projects easier and improve the old ones at the same time. 

    Hence, you can use Node.js for easy web application developments. If you want more streamlined experience, you can change the source code of this open-source runtime environment and use it on your system. Sign up for our Java full-stack developer course to learn everything about this versatile platform and specialize in this single programming language, JavaScript, for front-end and back-end development. 

    Now, let’s start with how to run Node.js on different operating systems. 

    How to Install Node.js on Windows?

    To install Node.js on your Windows PC, you need to first download the Windows Installer (.msi) package from its official website. This package contains all the files required for installing, updating, and modifying the downloaded Node.js version as per your preferences. So, check out the below steps to learn how to start Node.js and work seamlessly with it on your PC. 

    Note: The .msi package contains npm (Node.js package manager) also. Thus, you don't have to install npm separately on your PC. 

    System Requirements 

    1. Windows 10 OS
    2. 4 GB RAM
    3. 10 GB free storage
    • First, click here to visit the Node.js official website. 
    • Next, navigate to Windows Installer (.msi) and download either the 32-bit or 64-bit version according to the architecture of your PC. 
    • Now, double-click on this .msi setup file to start with the installation. 
    • On the Node.js Setup window, check the box next to I accept the terms in the License Agreement. 
    • Then, hit the Next button. 
    • Browse and select the path now where you want to install Node.js locally and click Next. 
    • On the Custom Setup window, make changes if you have any preference; if not, continue by clicking the Next button. 
    • If you want to automatically install necessary tools for native modules from Node.js, check the box on this screen and click Next. 
    • Finally, Node.js will prepare the installation; click the Install button on the Ready to install Node.js window. 
    • Now, Node.js will be installed on your system within a few minutes; click the Finish button to close the installer. 
    • You can verify the installation by running the below command in Command Prompt as an administrator, 
    node --version 
    npm --version

    How to Run Node.js Application Code on Windows? 

    After you install Node.js on your computer, 'node' will be registered as an internal command on your system. Then, you can easily run Node.js projects on Windows using Command Prompt. Check the below steps to learn how to run scripts on Node.js while using Windows OS.

    • First, open Command Prompt with administrative privileges.
    • Next, type in the below command and hit enter to create the test-node.js file (you can choose any name).
    echo console.log(1+1); >> test-node.js
    • This file will contain a simple application and print out 1+1.
    • Next, type 'node' along with the name of the application ('test-node.js' in this case) and press Enter.
    node test-node.js
    • Now, the result of running the app will be displayed on your command prompt.

    How to Install Node.js on macOS? 

    To install Node.js on macOS, you need to download the suitable installer package manager from the official website first. This package also contains an npm repository, so you don't need to install it separately on Mac. Take a look at the steps below to know how to start Node.js on macOS.

    System Requirements

    1. macOS 10.10 or higher
    2. 4 GB RAM
    3. 10 GB free Storage
    • First, click here to visit the Node.js official website.
    • Next, download the 64-bit macOS Installer (.pkg) file on your computer.
    • Double-click on the package; the Install Node.js window will open now.
    • Click the Continue button to start the installation process.
    • Now, go through the Software License Agreement and click Continue.
    • You'll get a prompt next; click the Agree button to accept the software license terms.
    • Then, select a destination where you want to install Node.js and click Continue.
    • On the next screen, hit the Install button and wait for it to get finished.
    • When done, click the Close button to turn off the installer.
    • Next, press the Command + Space buttons to open the search tab.
    • Search for 'terminal' and click on it from the results to open.
    • Now, check the Node.js version you've installed by entering the below command,
    node --version
    • Then, check the npm version with the below command,
    npm --version

    How to Run Node.js Files on macOS? 

    If you've already installed Node.js on macOS, you can run any file written on JavaScript. But for that, first, you have to create a file with a specific command. Here we've mentioned a step-by-step guide to help you know how to run the Node.js application on Mac; read now.

    • First, press the Command + Space button to open Spotlight Search and type in 'terminal'.
    • Next, open Terminal from the search results, enter the following command to create a file 'test-node.js' (you can choose any name), and hit Return,
    echo "console.log(1+1);" >> test-node.js
    • It'll contain an application, which will print out result 1+1.
    • Now, type in the below node command along with the file name and press Return.
    node test-node.js
    • You'll get the output now in the terminal.

    How to Run Node.js on Linux? 

    Being an open-source operating system, Linux has a wide range of distributions. Node.js installation is different on each Linux distro. You can check the Node.js Binary Archive to get the exact installation method for the system you're using. We’ve prepared this tutorial on how to start Node.js on a Ubuntu system. Go through now.

    • First, install the curl command-line utility on your Linux; enter the below command in a terminal window for that.
    sudo apt install curl
    • It may ask for your system password to confirm if you've admin access.
    • Once you enter that, the curl command will be installed.
    • Next, you need to copy the installation command for your Linux distro from the Node.js Binary Archive page and paste it into the terminal window.
    • For Ubuntu, we've used the below command (replace ‘14.x’ with the Node.js version you require.)
    curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
    • Now, the Node.js installation process will run.
    • When all Node.js libraries are installed on your system, enter the below command,
    sudo apt-get install -y nodejs
    • Finally, Node.js will be installed on your system.
    • Enter the Clear command now to clear your terminal.
    • Now, confirm your Node.js version by entering the below command in a terminal,
    Node --version
    • You can also check your npm version with the below command,
    npm –version

    How to Install Nodemon?

    Node Monitor (nodemon) is a command-line tool that makes back-end development a lot easier while using Node.js. It automatically restarts your node application whenever any file changes are detected within the directory. If you've already installed Node.js and the npm package on your system, you can install nodemon in two different ways.

    • Install Nodemon Globally: To install nodemon globally, run the "npm install --global nodemon" command on your system. It'll be installed on your system path, enabling you to use the "nodemon" command directly.  
    • Install Nodemon Locally: To install nodemon locally as a development dependency, run the "npm install --save-dev nodemon" command. If you perform a local installation, you can't use "nodemon" directly on the command line as you'll get the "command not found" output. You can only run it by calling it from within an npm script, i.e., npm start, or using npx nodemon.

    How to Run Node.js Project? 

    You can run Node.js projects using the node command. But first, you need to install Node.js on your system to get the node command. Next, you've to create the Node.js file using the methods mentioned above. Then, you can execute the file in the shell, even with certain modifications, if you want to. [We've used the "test-node.js" file for this tutorial.]

    • You can call your application file by typing in the below node command,
    node test-node.js
    • If you want to tell Node.js how to run the script using which interpreter, you can embed the node command with a 'shebang' line into your JavaScript; enter the below command for that.
    #!/usr/bin/node
    • In case your system doesn't have the node within the bin folder, it must have env, and you can instruct your OS to run the script with env while keeping the node as a parameter; use the below commands for that. [Enter your script in the place of the demo code.]
    #!/usr/bin/env node
    // demo code

    Note: The "shebang" is the first line in your JavaScript file. It can be modified to tell your OS which interpreter to use while running a script. But your file should have executable permission to use a shebang line. Use the below command to give executable permission to your file.

    chmod u+x test-node.js

    How to Install Node.js with nvm Installer?

    Node.js Version Manager (nvm) enables you to install and manage different versions of Node.js on a single local system. Even if you need a particular version, you can use the nvm installer to get that, as nvm will help you switch between all the existing versions on the system depending on what your project demands.

    Here we've provided a step-by-step guide on how to install Node.js with the nvm installer on macOS and Linux; have a look. If you're using a Windows PC, check this guide.

    • First, install curl on your system by entering the below command in a terminal window.
    $sudo apt update && sudo apt install curl -y
    • Next, download the nvm installation script by using the below command (replace v0.35.0 with the latest nvm version.)
    $curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh -o install_nvm.sh
    • Now, type the below command and hit enter to run the install script.
    bash install_nvm.sh
    • To apply the changes on your profile and set the right environment variables for running nvm, enter the below command.
    CentOS, Redhat, and Fedora users:
    $source ~/.bashrc
    Debian users:
    $source ~/.profile
    • Once done, preview the list of available Node.js versions by entering the below command.
    $ nvm ls-remote
    • Now, choose a specific version and install it by entering the below command (replace 16.14.0 with your required Node.js version.)
    $nvm install v16.14.0

    How to Install npm in Visual Studio Code? 

    Visual Studio Solution has been helping full-stack developers with easy npm package management on Node.js. You can issue npm commands through its intuitive UI and perform different development tasks. Here we've mentioned how to install npm in Visual Studio Code using an extension, Solution Explorer, which adds a Visual Studio Solution File explorer panel within it. Let's start!

    • First, you must access the npm package installation window from Solution Explorer.
    • For that, you've to right-click on the npm node in the project and choose to Install New npm Packages from the pop-up window.
    • In the installation window, you can pick any package and specify Options for it as mentioned below:
      • Dependency type - Choose Standard if you want the package to be a runtime dependency and Development if you want to use it for development only.
      • Add to package.json - Check the box next to (recommend).
      • Selected version - Select (latest).
      • Other npm arguments - If you want to install any specific version, add the value here.
    • Once done, click the Install Package button.
    • Next, navigate to View> Output to check the progress of the npm installation in the Output window.

    How to Start Node Server?

    You might know that Node.js can be used to run desktop app servers seamlessly. Here we've provided a guide on ‘Node.js on how to start a server’ and run it on your local system. Have a look and create your first Node.js web server.

    • First, visit this link and download the Node.js LTS version.
    • Next, check your node version by entering the below command,
    node --version
    • Then, create a new folder using the below command (change testApp with your project),
    mkdir testApp
    • Now, move to the just created folder using the below command,
    cd testApp
    • After that, you have to initialize the npm package installation using the following command,
    npm init -y
    • Now, create a .js file (test-node.js in our case) and write the following code to create a server and ask it to listen to a certain port that will preclude the HTTP library.
    const http = require('http')
    const port = 8080
    
    // Create a server object:
    const server = http.createServer(function (req, res) {
    
    // Write a response to the client
    res.write('Hello World')
    
    // End the response
    res.end()
    })
    
    // Set up our server so it will listen on the port
    server.listen(port, function (error) {
    
    // Checking any error occur while listening on port
    if (error) {
    console.log('Something went wrong', error);
    }
    // Else sent message of listening
    else {
    console.log('Server is listening on port' + port);
    }
    })
    • After that, run the application by entering the below command,
    • node test-node.js
    • Now, launch your browser and visit http://localhost:8080/.
    • You must see the "Hello World" output.

    Looking for the best Python course with placement? Look no further! Discover the power of Python with our unique and catchy content. Join us today and unlock endless possibilities in the world of programming. Don't miss out!

    Conclusion 

    Now you know how to install Node.js on Windows, macOS, and Linux, along with all the basic knowledge you need to get started with back-end development. Sign up for our KnowledgeHut’s Node.js program to ace full-stack development.

    Frequently Asked Questions (FAQs)

    1Can Node.js Run on Linux?

    Yes, Node.js can easily run on Linux. Since it's an open-source platform, you can modify the source code and use it on your Linux distro to get the best experience. Find the exact installation command for the Linux distro you're using frohere. 

    Check the installation method we've mentioned above to run Node.js on Ubuntu.

    2How do I Run a Node.js File on a Mac?

    To run a Node.js file on Mac, you need to download the installer package from the Node.js official website first and set it up by following the on-screen instructions on your system. Then, you've to create a .js file and run it using the node command on Mac.

    Check the above-mentioned step-by-step guide to install Node.js on macOS and run a file on it.

    3How do I Run npm on Windows?

    To run npm on Windows, you need to download the Node.js installer package first. The npm package comes with the installation package only. So, installing Node.js on your PC will automatically install npm alongside. Check the above section to know how to run Node.js on Windows.

    4How to Install npm on Linux?

    You need to use specific commands to install npm on different Linux distros. Check the complete list below- (use the commands without quotation)

    • Ubuntu, Debian, and Linux Mint: "$ sudo apt install npm" or "$ sudo apt install npm nodejs"
    • CentOS 6 and 7, and older versions of Red Hat: "$ sudo yum install epel-release $ sudo yum install npm"
    • CentOS 8 (and newer), Fedora, and Red Hat: "$ sudo dnf install npm"
    • Arch Linux and Manjaro: "$ sudo pacman -S npm"
    • OpenSUSE: "$ sudo zypper install npm"

    That's all! Our guide on how to install Node.js on Windows, macOS, and Linux ends here. If you've further queries, drop us a comment. Thanks for reading!

    5Can I Run Node.js on Windows?

    Yes, you can run Node.js on Windows. You need to download the 32-bit/64-bit installer package first from the Node.js website and install it on your PC. You can find the exact installation method in the above section.

    Profile

    Sachin Bhatnagar

    Program Director, FSD

    With 20+ yrs of industry experience in media, entertainment and web tech, Sachin brings expertise in hands-on training and developing forward-thinking, industry-centric curricula. 30k+ students have enrolled in his tech courses.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon