Wednesday, October 15, 2014

Installing Node.js on Windows - Six Revisions

Installing Node.js on Windows - Six Revisions


Installing Node.js on Windows

Posted: 15 Oct 2014 03:00 AM PDT

Many powerful open source projects have Node.js as a dependency, so often times you’ll need to set it up on your web server.

But what if you wanted to develop and test your Node-dependent projects in your Windows PC before putting it up on the server?

Setting up Node.js locally is quite simple. In this tutorial, I’ll walk you through the following:

  1. Installing Node.js on Windows
  2. Running a Node app in your browser
  3. Installing Node packaged modules via npm
  4. Uninstalling Node from Windows

A note on nomenclature: The official name of the project is Node.js or node.js, and many people refer to it simply as Node or node. Throughout the tutorial, these terms will be used interchangeably.

Installation

Node has a Windows and Mac installer, as well as binaries for Linux users. We’ll be going over how to install Node on a Windows 7 personal computer, but the general process is the same regardless of which operating system and OS version you’re using.

To start, head over to the Downloads page of Node.js. Click on the installer you would like to download and install. For this tutorial, we’re using the 64-bit Windows Installer.

Node.js Download page

Click on the installer file from your computer to open up the Node.js Setup Wizard.

Node.js Setup wizard

Just go through the Setup Wizard until Node has finished installing.

Node.js Setup wizard installing Node.js

Node.js Setup completed the Node.js installation

I promise you, that’s it: You’re done installing Node.js as well as npm. (The latter is installed with Node.js by default.)

Running a Node App

After installing the Node.js platform, the next thing you will want to do is run a Node app from your computer. We’ll create a simple app for demonstration purposes.

First, create a folder for your app. For my setup, I have a folder named projects in my Desktop folder. Inside the projects folder, I created a subfolder named my-node-app.

"my-node-app" folder

Inside the my-node-app folder, create a file called hello-world.js and place the following source code in it:

var http = require('http');    http.createServer(function (request, response) {    response.writeHead(200, {'Content-Type': 'text/plain'});    response.end('Hello World\n');  }).listen(3000);    console.log('Server running at http://localhost:3000/');  

The above code is a slightly modified version of Node’s web server example, which will create an HTTP web server in our Windows computer.

We’re now ready to run our first Node.js app.

Open the Node.js command prompt, which you can do via the Windows Start menu. If you can’t find it pinned in your Start menu, just type "node.js" in the Search programs and files input field to find it.

Finding the Node.js command prompt

The Node.js command prompt is a command line interface (CLI):

Initial screen of the Node.js command prompt.

Using the CLI, navigate into your Node project’s folder. The one I set up earlier has this file path: Desktop\projects\my-node-app so to change into my folder, I used the cd (change directory) command like so:

cd Desktop\projects\my-node-app

Node.js command prompt results of cd command.

Next, let’s run hello-world.js. Simply type the node command like so:

node hello-world.js

If all went well, we’ll see the text "Server running at http://localhost:3000/" in the command prompt.

Node.js command prompt indicating that the server is running.

Use your favorite web browser to navigate to this URL:

http://localhost:3000/

You should see the text "Hello World" in the browser window, indicating that everything’s good to go.

A Node.js app running on the browser via localhost.

Installing Node Packages via npm

Packaged modules make Node.js a much more powerful web app development platform. Think of Node packaged modules as being similar to plugins for jQuery and WordPress, or browser extensions for Chrome, or Gems for Rails; they extend your Node.js installation and/or provide pre-built functionality for your Node.js apps.

npm is the default package manager of Node.js. It’s automatically installed along with Node.js by default.

Let’s say we wanted to extend our Node.js installation with a MySQL driver so we can use a database for our app. In the Node.js command prompt, just issue the following command:

npm install mysql

The above command will download and install the mysql node package from npm. After the package installation is complete, you’ll see something like this:

mysql node packaged module installation completed.

To find other node packaged modules you might want to install, head over to the npm website.

Troubleshooting

When you try to install a package, you might encounter an error message like this:

Error: ENOENT, stat 'C:\\Users\your-name\AppData\Roaming\npm'

The solution to the issue is to manually create the npm folder in the path that’s being displayed by the error message: Go to your AppData\Roaming folder and simply create a new folder called npm inside of it. Afterwards, issue the npm install command again to complete the installation.

Uninstalling Node

You can uninstall Node.js just like any other software in your computer. Go to Start > Control Panel > Programs and Features and then locate Node.js. Click on Node.js, press the Uninstall button, and then just follow the prompts in the dialog window that appears.

Uninstalling Node.js from Windows 7.

Related Content

About the Author

Example of a website with a large background image: Jacob Gube is the founder of Six Revisions. He’s a front-end web developer by profession. If you’d like to connect with him, head on over to the contact page or follow him on Twitter: @sixrevisions.

The post Installing Node.js on Windows appeared first on Six Revisions.

No comments:

Post a Comment