Node.js overview

Node.js is a newer runtime environment that has become very popular among web developers. It uses JavaScript, traditionally a front end programming language on the server side.

DreamHost does not support Node.js on shared web servers, as the security setup on DreamHost shared servers is incompatible with compiling or running Node.js.

If you try to compile Node.js on one of the shared web servers, your user will automatically be banned through grsec (taking down all the PHP websites that run under that user) and the server will have to be rebooted before your user can be unbanned. If you do it one more time, you will be forced to move to a VPS.

DreamHost's VPS and Dedicated servers can run Node.js just fine.

Enabling Node.js using the DreamHost panel

Visit the following article to learn how to enable Node.js in the panel:

Installing a custom version of NVM and Node.js

DreamHost support is unable to assist with custom installations. The directions below will explain how to install Node.js but if you find anything is not working as intended, you must contact a developer for further assistance.

The following steps use Node Version Manager to install.

  1. Check the version of Passenger running on your private server. The server must run version 4.0.37 or later.
    [server]$ dpkg -l | grep passenger
    
  2. Visit the following page to determine what version of NVM to install:
  3. Run the following command to download NVM. Change the version number as needed:
    [server]$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
    • This command installs NVM into a new directory under your user named /.nvm
    • This command also adds the following to your .bashrc file:
    export NVM_DIR="/home/exampleuser/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
    
  4. Insert the following into your .bash_profile file:
    [server]$ . $HOME/.bashrc
  5. Run the following to allow your current Shell session to use NVM:
    [server]$ source .bash_profile
  6. To test if you can now use NVM, run the following which should respond with the version you have installed:
    [server]$ nvm --version
    0.33.0
    
  7. Install any version of Node.js you wish:
    [server]$ nvm install 7.5.0
  8. Check which version of Node.js is running by entering the following:
    [server]$ node -v
    v7.5.0
    

Node Package Manager

At this point you have NVM and Node.js installed and active. Node Package Manager is also installed with Node.js. Visit the following page for further details on NPM:

Check the version by running the following command:

[server]$ npm -v

To update npm to the latest version, run the following command:

[server]$ npm install npm -g

Check the directory NPM writes to. Run the following command:

[server]$ npm config get prefix
/home/exampleuser/.nvm/versions/node/v0.12.2

If you installed NVM as shown above, you'll see the directory response as the /.nvm folder under your current user.

Installing packages

For information on how to install new packages using NPM, visit the following link:

Testing Node.js

Now that you have installed Node.js you can create a simple test script to confirm if it's functioning.

Simple 'Hello World!' script

Create and run this script within your terminal:

  1. Create a file named HelloWorld.js, and then add the following in the file:
    console.log('Hello World!');
  2. Run the following in your terminal to run the script:
    [server]$ node HelloWorld.js
    Hello World!
    

    If correctly installed you will see the console respond with Hello World!

Website example

You can also create a basic HTTP server to load a site.

  1. Create a file in your web directory titled server.js, and then add the following code in the file:
    var http = require("http");
    
    http.createServer(function(request, response) {
      response.writeHead(200, {"Content-Type": "text/plain"});
      response.write("Hello World");
      response.end();
    }).listen(8888);
    
    • This HTTP server will listen on port 8888.
  2. Start the server by running the following:
    [server]$ node server.js
  3. Visit the page in a browser with port 8888 appended to the end. For example, http://example.com:8888.

    If running correctly, your site will display the text Hello World!.

Using Node.js with Passenger

If you're using Node.js with Passenger you need to add the following to your site's .htaccess file:

PassengerNodejs /home/USERNAME/.nvm/VERSION/node/bin/node

Make sure to change USERNAME to your actual shell user and VERSION to the version you're using.

See also

Internal links

External links