Installing a custom version of NVM and Node.js

Overview

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. Make sure you're in your user's directory.
    [server]$ cd ~
  4. Run the following command to download NVM. Change the version number as needed:
    [server]$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.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/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
  5. View the creating and editing a file via SSH article for instructions on how to edit your existing .bash_profile. In this file, add the following line.
    source ~/.bashrc
  6. Run the following command to allow your shell to use this new version of nvm:
    [server]$ . ~/.bash_profile
  7. To test if you can now use NVM, run the following which should respond with the version you have installed:
    [server]$ nvm --version
    0.34.0
    
  8. Check which versions of Node.js are available:
    [server]$ nvm ls-remote
  9. Install any version of Node.js you wish:
    [server]$ nvm install 10.15.1
  10. Check which version of Node.js is running by entering the following:
    [server]$ node -v
    v10.15.1
    

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@latest -g

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

[server]$ npm config get prefix
/home/exampleuser/.nvm/versions/node/v6.10.0

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

Installing packages

View the following page for instructions on how to install packages locally or globally:

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. If you have enabled Node.js in the panel, you can just visit your site to see the file functioning. If you have not enabled Node.js in the panel, 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!.

Troubleshooting

Passenger is not using your custom version of Node.js

If you find the custom version you installed is not being used, add the following code to an .htaccess file in your project's directory:

PassengerNodejs /home/username/.nvm/versions/node/v10.15.1/bin/node

Make sure to change the username and version to the version you have installed.

Next, reload Passenger my running the following command in the app's directory:

[server]$ mkdir tmp && touch restart.txt

In the future, you just need to touch the restart.txt file to restart the application.

[server]$ touch tmp/restart.txt

View the following page for further details:

Did this article answer your questions?

Article last updated PST.