Node.js

Node.js (also known as “Node”) is a platform for developing and running applications with JavaScript. You can install a Node.js application with the control panel.

Note

If you are working with a Node.js application during an SSH session, then you may find it easier to add the application’s bin directory to your PATH environment variable. Otherwise, you must use full paths to node, npm, and other executables for the application.

To add a Node.js application’s bin directory to your PATH environment variable:

  1. Switch to your Node.js application’s directory. Enter cd $HOME/webapps/nodejs_app/, where nodejs_app is the name of your Node.js application as it appears on the control panel, and press Enter.
  2. Add the application’s bin directory to your PATH environment variable. Enter export PATH=$PWD/bin/:$PATH and press Enter.

For the remainder of the SSH session, you can run your application’s executables without entering a full path.

Starting and Stopping Node.js

To start or stop a Node.js application:

  1. Open an SSH session to your account.
  2. Run the start or stop script:
    • To start the Node.js application, enter $HOME/nodejs_app/bin/start, where nodejs_app is the name of your Node.js application as it appears on the control panel, and press Enter.
    • To stop the Node.js application, enter $HOME/nodejs_app/bin/stop, where nodejs_app is the name of your Node.js application as it appears on the control panel, and press Enter.

Note

Node.js applications are created with a cron job that starts the Node.js application every twenty minutes, if it’s not already running. If you do not want the Node.js application to restarted eventually, then you must modify your crontab. For more about cron, see Scheduling Tasks with Cron.

Installing Packages with npm

npm is the Node.js package management tool. To install a package with npm:

  1. Open an SSH session to your account.

  2. Switch to your Node.js application’s directory. Enter cd $HOME/webapps/nodejs_app/, where nodejs_app is the name of your Node.js application as it appears on the control panel, and press Enter.

  3. Add the application’s bin directory to your PATH environment variable. Enter export PATH=$PWD/bin/:$PATH and press Enter.

  4. Enter npm install -g package, where package is the name of the package to be installed, and press Enter.

    Note

    npm can install packages for the current directory (in ./node_modules/), or for the entire Node.js application (in ~/webapps/nodejs_app/lib/node_modules/). This step assumes packages should be made available to all Node.js programs used with the current application. To install the module for the current directory only, omit the -g flag before running the command.

The package is installed.

Upgrading Node.js

You can use n, a Node.js version manager, to upgrade a Node.js application to a newer version of Node.js. To upgrade a Node.js application to the latest stable version of Node.js:

  1. Open an SSH session to your account.

  2. Switch to your Node.js application’s directory. Enter cd $HOME/webapps/nodejs_app/, where nodejs_app is the name of your Node.js application as it appears on the control panel, and press Enter.

  3. Add the application’s bin directory to your PATH environment variable. Enter export PATH=$PWD/bin/:$PATH and press Enter.

  4. Enter npm cache clean -f and press Enter.

  5. Install the Node.js version manager. Enter npm install -g n and press Enter.

  6. Upgrade Node.js. Enter N_PREFIX=$PWD n stable and press Enter.

    Alternatively, you can substitute stable with a version number, like 4.2.2, to install a specific version of Node.js.

The version of Node.js you selected is installed. To confirm your current version of Node.js, enter node --version and press Enter.


Search the documentation


Example: "configure email"
or "create database"