Search the CLDY knowledge base

Updated on August 24, 2022

How to Create a Node.js application with cPanel using the Node.js Selector

For you to be able to use Node.js* applications via cPanel, you will need to run the Node.js Selector. We will help you install this using the methods listed below.

Before you proceed, take note of the following terms used within this article:

  • app: application root directory and application URL.
  • app.js: application startup file.
  • example: username.
  • example.com: domain.
  • My app: the description.
  • 10 or 11: node.js major version number.

Make sure to alter the values as needed depending on the account and the application.

 

Method #1: Set up the Node.js application using the cPanel interface

Step #1: Create the application

  1. Log in to cPanel.
  2. From your cPanel cPanel home screen, go to the Software block and click on Setup Node.js App.
  3. Click on Create Application.
  4. Complete the following fields on the succeeding page:

    • Node.js version: A dropdown will be available for you to choose a version you want to use.
    • Application mode: You will have two options: Development or Production. (You can use Development as you start and change this to Production at a later time.)
    • Application root: This is the location for your application files. Make sure to append /home/username to complete the path to the application files in the cPanel home directory. (Example locations: /home/username/appname or /home/username/apps/appname)
    • Application URL: This is the web address of the application.
    • Application startup file: This will be the initial file that will run as the application opens.
  5. Make sure to complete all the required details in the form, and click on Create.

  6. The application should start right away. Click on Open for you to see a test version.

    You should see the It Works! message appear.

 

By using the package.json settings file and the npm package manager, you can now work on improving the interface of the application itself by following the next steps:

 

Step #2: Create the package.json file

  1. Go to your cPanel homepage.
  2. Under Files, click on File Manager.
  3. On the left side of the page, click on the application root location (see step 4 from earlier):
  4. Click on +File.
  5. For the file name, type in package.json,  and click on Create New File.
  6. The package.json file should now be visible. Right-click on the created file and click on Edit.
  7. A pop-up dialog box will appear. Click on OK.
  8. On the editor screen, type in the following:
    {
      "name": "app",
      "version": "1.0.0",
      "description": "My App",
      "main": "app.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC"
    }
  9. Click on Save Changes.
  10. You can now click on Close.

 

Step #3: Install npm

 

  1. Once again, go to your cPanel homepage and click on Setup Node.js App. This should be under the Software block.
  2. Under Actions, click on the pencil (Edit the application) icon.
  3. Click on Run NPM Install
  4. You should receive a prompt saying that NPM install has been completed successfully.
  5. On the application setup page, you can choose to “Enter the virtual environment” where you can add and install packages with NPM through SSH login. This command should be visible in the information box within the application setup page.

 

Method #2: Set up the Node.js application using the command line

If you are more comfortable using SSH than cPanel, these are the steps for you:

  1. Log in using SSH.
  2. Use this command to create the application:
    cloudlinux-selector create --json --interpreter nodejs --version 11 --app-root app --domain example.com --app-uri app
  3. This should be the application directory:
    cd ~/app
  4. Create the package.json file using this command:
    vi package.json
  5. From your keyboard, press i to change to insert mode. Add the following text into the editor:
     {
        "name": "app",
        "version": "1.0.0",
        "description": "My App",
        "main": "app.js",
        "scripts": {
          "test": "echo \"Error: no test specified\" && exit 1"
        },
        "author": "",
        "license": "ISC"
      }
  6. Press the Esc (escape key) and then : (colon) to switch to command mode.
  7. Press x and then Enter which will save your work and will let you exit simultaneously.
  8. Next, install the npm using:
    cloudlinux-selector install-modules --json --interpreter nodejs --user example --app-root app

    Use this command to add or install other packages with npm: (you will then enter a virtual environment – see step 5 above)

    source /home/example/nodevenv/app/11/bin/activate && cd /home/example/app
  9. As you run the application, here are the commands you can use:stop:
    cloudlinux-selector stop --json --interpreter nodejs --app-root ~/app

    start:

    cloudlinux-selector start --json --interpreter nodejs --app-root ~/app

    restart:

    cloudlinux-selector restart --json --interpreter nodejs --app-root ~/app

 

* NOTE : Node.js Selector is only available on VPS hosting. 

Still can't find what you're looking for?