Running a Node.js app on cPanel hosting: setup, upload and npm

Print
  • 0

In short: Node.js apps are started from cPanel with the Setup Node.js App tool. You create the app, upload your project, install npm packages from the interface or the Terminal, and restart. Node.js comes with the Web Hosting SSD plans.

What to have ready

  • Your project, including package.json and the file that starts it (such as app.js, server.js or index.js).
  • The domain or subdomain the app will answer on, already added in cPanel.
  • The Node.js version you developed and tested with.

Step 1. Create the application

  1. Log in to cPanel and, under Software, open Setup Node.js App. Any existing apps are listed there.
  2. Click Create Application.
  3. Fill in the form:
FieldWhat to enter
Node.js versionThe version your project needs.
Application modeProduction for a live site, Development for testing only.
Application rootThe folder holding the app's files, for example nodeapp.
Application URLThe domain, plus an optional path, where the app will be served.
Application startup fileThe entry file, for example app.js.

Click Create. A sample app is placed in the folder. Open the app's address in a browser; if the sample page loads, the environment is working.

Watch out: the app takes over the address you give it. Point it at the root of your main domain and the site that lived there stops showing. Use a subdomain or a separate path while you experiment.

Step 2. Upload your project

  1. Delete the sample files that were generated in the app folder.
  2. Upload your project into that folder with File Manager or FTP.
  3. If your entry file has a different name, click Edit on the app and correct Application startup file.
  4. Once a package.json is present, a Run NPM Install button appears on the app page. Click it to install the dependencies.
  5. Click Restart and load the app's address in your browser.

Common mistake: uploading the node_modules folder from your computer. On the server, packages are installed into a separate virtual environment, and node_modules in the app folder is only a link to it. A real folder with that name blocks the installation.

Step 3. Managing npm packages from the Terminal

The install button does not always succeed, especially for projects with many dependencies. In that case use the command line:

  1. Open Terminal in cPanel or connect over SSH. SSH access on shared hosting is enabled by our support team on request.
  2. At the top of the app's page in Setup Node.js App you will see a command that enters the app's virtual environment. Copy it and run it. It looks like this:
    source /home/USERNAME/nodevenv/nodeapp/VERSION/bin/activate && cd /home/USERNAME/nodeapp
  3. Run npm install, then restart the app in cPanel.

Handy npm commands

CommandPurpose
npm installInstalls everything listed in package.json
npm install NAMEAdds one package
npm uninstall NAMERemoves a package
npm update NAMEMoves a package to a newer version allowed by package.json
npm list --depth=0Lists the packages installed directly in the project
npm outdatedShows packages that have newer releases
npm helpPrints help and the list of commands

If the app will not start

  • Open Edit and double-check the startup file name and the app folder.
  • Read the app's error log: the form lets you set a log file where Node.js writes its error messages.
  • Keep in mind that the app runs within your plan's resources (CPU, memory, processes). A process that uses too much is stopped by the account limits.

Still stuck? Include the domain and the name of the app folder when you open a ticket, so we can look at it straight away.

Related articles

Was this answer helpful?