Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
09.10.2024

How to Install and Configure Node.js and NPM on Windows

Node.js is a powerful JavaScript runtime built on Chrome’s V8 engine, and NPM (Node Package Manager) is a package manager that comes with Node.js. They are essential tools for developing modern JavaScript applications. This guide will walk you through the process of installing and configuring Node.js and NPM on a Windows system.

Step 1: Download Node.js Installer

  1. Visit the Official Node.js Website:Go to the official Node.js website.
  2. Choose the Correct Version:
    • LTS (Long-Term Support): Recommended for most users, as it is more stable.
    • Current: Includes the latest features but may be less stable.

    Download the appropriate installer for your Windows version (e.g., 64-bit).

Step 2: Install Node.js and NPM

  1. Run the Installer:
    • Double-click the downloaded node-vxx.x.x-x64.msi file to start the installation process.
  2. Follow the Setup Wizard:
    • Click Next on the welcome screen.
    • Accept the License Agreement and click Next.
    • Choose the installation path (by default, it is C:\Program Files\nodejs\) and click Next.
    • On the Custom Setup screen, make sure to select the option “Install NPM” along with Node.js, then click Next.
    • Click Install to start the installation.
    • Click Finish when the installation completes.

    By default, the installer adds Node.js to your system’s PATH environment variable, which means you can use node and npm commands in the command prompt without additional configuration.

Step 3: Verify Installation

After the installation, it is important to verify that Node.js and NPM are installed correctly.

  1. Open Command Prompt:Press Windows + R, type cmd, and press Enter.
  2. Check Node.js Version:Run the following command to check the installed version of Node.js:
    node -v

    This should display the version number of Node.js (e.g., v18.x.x).

  3. Check NPM Version:Verify the installation of NPM by running:
    npm -v

    This should display the version number of NPM (e.g., 9.x.x).

Step 4: Update NPM (Optional)

Although NPM is installed with Node.js, it may not always be the latest version. You can update NPM to the latest version using the following command:

npm install -g npm@latest

The -g flag installs the package globally, ensuring that NPM is updated system-wide.

Step 5: Configure NPM (Optional)

You can configure NPM settings for a more customized experience. Here are some useful configurations:

  1. Set the Default Directory for Global Packages:By default, global packages are installed in the C:\Users\<username>\AppData\Roaming\npm directory. You can change this if needed:
    npm config set prefix “C:\your\preferred\path”
  2. Set the Default Registry:If you want to use a custom registry, such as a private NPM registry, set it with:
    npm config set registry “https://your-custom-registry.com/”
  3. View All Configurations:To see all current NPM configurations, run:
    npm config list

Step 6: Create and Run a Simple Node.js Application

  1. Create a New Directory:In the command prompt, create a new directory for your project and navigate into it:
    mkdir my-node-app
    cd my-node-app
  2. Initialize a New Node.js Project:Create a package.json file by running:
    npm init -y

    This will generate a default package.json file with the basic configuration for your Node.js project.

  3. Create an app.js File:Inside the my-node-app directory, create a new file named app.js using any text editor (e.g., Notepad, Visual Studio Code).Add the following code to app.js:
    // app.js
    const http = require(‘http’);const hostname = ‘127.0.0.1’;
    const port = 3000;const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader(‘Content-Type’, ‘text/plain’);
    res.end(‘Hello, World!\n’);
    });

    server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
    });

  4. Run the Node.js Application:In the command prompt, run the application:
    node app.js

    You should see the message:

    Server running at http://127.0.0.1:3000/
  5. Test the Application:Open a web browser and go to http://127.0.0.1:3000/. You should see the message Hello, World! displayed in your browser.

Step 7: Install and Use Node.js Packages

You can install additional Node.js packages using NPM. For example, install the popular Express framework:

npm install express

After installation, you can use Express in your Node.js application by requiring it in your app.js file:

const express = require(‘express’);
const app = express();
const port = 3000;app.get(‘/’, (req, res) => {
res.send(‘Hello from Express!’);
});app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});

Run the updated app.js:

node app.js

Visit http://localhost:3000/ in your browser to see the message from Express.

Conclusion

You have now successfully installed Node.js and NPM on a Windows system, configured NPM, and created a simple Node.js application. With these tools, you can start building, managing, and deploying JavaScript applications on your Windows machine. Happy coding!

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills