Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Installing and Configuring Node.js on Windows

Tech 2

Node.js Overview and Download

1. Runtime Environments, Compilers, and Development Tools

A runtime environment provides the necessary components to execute code, including a core compiler. Node.js is a JavaScript runtime environment. In contrast, an Integrated Development Environment (IDE) like Visual Studio Code or PyCharm is a development tool focused on writing, debugging, and managing code. An IDE typically invokes a separate runtime environment to execute programs. The relationship can be summarized as: Development Environment > Runtime Environment > Compiler.

2. Node.js, JavaScript, and TypeScript

JavaScript is a lightweight, interpreted programming language. It requires a runtime environment like a web browser or Node.js to be executed. TypeScript is a superset of JavaScript that adds static typing. The TypeScript compiler transpiles TypeScript code into JavaScript, which then runs in a browser or Node.js environment.

3. Node.js vs. Web Browsers

A web browser is a client-side application that can execute JavaScript, acting as a client-side runtime environment. Node.js is a server-side runtime environment built on Chrome's V8 JavaScript engine, allowing JavaScript to run outside a browser, similar to languages like Python or Java.

Download Link: https://nodejs.org

Downloading the Node.js Installer

Navigate to the official Node.js website. The homepage typically offers two primary download options for Windows: an LTS (Long-Term Support) version and a Current version. For most users, the LTS version is recommended for stability.

Click the "Downloads" link to access a page with options for various operating systems (Windows, macOS, Linux) and architectures.

Understanding Download Options

  • Installer vs. Binary vs. Source: Choose an Installer (e.g., .msi for Windows) for a guided setup or a Binary archive for manual installation. Source code requires compilation.
  • File Formats: .msi (Windows installer), .pkg (macOS), .tar.gz or .zip (compressed archives).
  • System Architecture: Select the version matching your system: 64-bit (x64) for most modern Windows PCs, 32-bit, or versions for ARM-based systems (ARM64).

For a standard Windows 64-bit system, download the Windows Installer (.msi) 64-bit file.

Installation and Setup Process

1. Launching the Installer

Run the downloaded .msi file. Accept the license agreement and proceed.

2. Choosing the Installation Directory

The default installation path is C:\Program Files\nodejs\. You can change this if desired. Click Next.

3. Customizing the Setup

The installer presents several features to install:

  1. Node.js runtime: The core execution environment.
  2. Corepack manager: A tool to manage multiple package managers (like npm, Yarn).
  3. npm package manager: The default Node.js package manager.
  4. Online documentation shortcuts: Adds links to Node.js docs in the Start Menu.
  5. Add to PATH: Adds Node.js and npm to the system's PATH environment variable, allowing you to run node and npm commands from any command prompt.

It is recommended to install all features, especially "Add to PATH". Use the default "Entire feature will be installed on local hard drive" for each.

4. Installling Tools for Native Modules

Some npm packages contain native code that needs to be compiled during installation. The installer may offer to "Automatically install the necessary tools", which includes Python, Visual Studio Build Tools, and the Chocolatey package manager for Windows.

You can choose to install these now for convenience, or skip and install them manually later if needed. Click Next.

5. Completing the Installation

Click Install. After the process finishes, click Finish. If you opted to install the build tools, a PowerShell window may open to handle their installation.

To verify the installation, open a Command Prompt (cmd.exe) and run:

node --version
npm --version

If both commands return version numbers, Node.js and npm are installed correctly.

Common Post-Installation Configuration

1. Changing the Global npm Paths

By default, npm installs globally-available packages and caches files in your user directory on the C: drive (%APPDATA%\npm). To change these locations (e.g., to D: drive), first create the target folders manually, such as D:\nodejs\global and D:\nodejs\cache.

Then, set the new paths using npm config commands in the terminal:

npm config set prefix "D:\nodejs\global"
npm config set cache "D:\nodejs\cache"

Finally, you must add the new global package directory to your system's PATH environment variable.

  1. Open System Properties > Advanced > Environment Variables.
  2. In the User variables section, find and edit the Path variable.
  3. Replace the existing npm path (e.g., %APPDATA%\npm) with your new path: D:\nodejs\global.

2. Configuring the Package Registry Mirror

The default npm registry is located overseas. For faster downloads in some regions, you can switch to a mirror, such as the one provided by npmmirror.com (formerly Taobao).

npm config set registry https://registry.npmmirror.com/

To verify the current registry:

npm config get registry

Alternatively, you can use the nrm (Node Registry Manager) tool to switch between registries easily.

# Install nrm globally
npm install -g nrm
# List available registries
nrm ls
# Use a specific registry (e.g., taobao)
nrm use taobao

3. Using Alternative Package Managers

While npm is the default, you can install and use others like yarn or cnpm.

# Install Yarn globally via npm
npm install -g yarn
# Install cnpm globally via npm
npm install -g cnpm
# Check their versions
yarn --version
cnpm --version

Running Node.js Applications

1. Using the Command Line

You can execute JavaScript files directly using the node command. Create a file named app.js:

function calculateSum() {
    const valueA = 5;
    const valueB = 12;
    console.log(`The sum is: ${valueA + valueB}`);
}
calculateSum();

Run it from the command line:

node app.js
# Output: The sum is: 17

For managing project dependencies, use npm init to create a package.json file in your project directory. This file records metadata and dependencies.

# Interactive setup
npm init
# Accept all defaults
npm init -y

2. Using an Integrated Development Environment (IDE)

IDEs like Visual Studio Code, WebStorm, or PyCharm provide enhanced tools for Node.js development, including code editing, debugging, and integrated terminals. You can create a .js file and run it using the IDE's built-in run commands or terminal, which internally calls the node command.

Tags: Node.js

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.