Using the Node.js Selector for Command-Line Scripts
Knowledgebase Article
}
Knowledgebase Article
If you need to run Node.js from the command line - for example, to install packages with npm, run build tools, or execute scripts - you might have already looked at our guide on using NVM to install Node.js. That method works well, but on servers with older operating systems, newer versions of Node.js may not function due to system library limitations.
In these cases, there’s an alternative: you can use the Node.js Selector in cPanel, which is typically used to run web apps, to create a containerised Node.js environment that also works perfectly for command-line tasks.
Although the Node.js Selector is designed to launch web-facing applications (as explained in our guide to creating web apps with Node.js Selector), it can also be used to run non-web scripts - you simply create a dummy application and use a special command to enter its environment.
This guide walks you through exactly how to do that.
Start by creating a directory in your home folder called node-scripts
. Ideally, place this above public_html
to ensure your scripts are not accessible via the web. This keeps your command-line scripts private and clearly separated from any website content.
This folder will be where you store all of your Node.js scripts, configuration files, and anything else related to your CLI-based Node.js work.
You can create the folder using:
mkdir ~/node-scripts
The Node.js Selector in cPanel is designed primarily to run web applications. For this reason, it requires each app to be associated with a domain or subdomain. This helps it set up the environment and configure the web server routing — even if you’re not actually using it to serve a website.
Since we’re only using this app for command-line tasks, we’ll create a throwaway subdomain like cli.yourdomain.com
. This is a workaround to meet the system's requirement for a domain link, even though we won’t be serving anything to the web.
When you create the subdomain in cPanel, it will automatically create a document root folder (e.g. /home/youruser/cli.domain.com
). This folder is normally where web files would go, but in our case, we won’t be using it. You can leave that folder completely empty.
Instead, during app setup, you’ll be able to manually specify the location of your actual scripts folder, such as /home/youruser/node-scripts
. This ensures your CLI scripts remain private and are not exposed to the web, while still allowing you to take full advantage of the containerised Node.js environment provided by the Selector.
In cPanel, open the Setup Node.js App tool and click Create Application. Configure it as follows:
node-scripts
(e.g. this will map it to /home/youruser/node-scripts
)Once created, the app page will display a command similar to the following:
source /home/youruser/nodevenv/node-scripts/20/bin/activate && cd /home/youruser/node-scripts
This does two things:
node-scripts
folder so you're ready to workYou will need to connect to SSH and run this command to enter the Node.js virtualised environment.
Note: If SSH is not yet enabled on your account, please contact our support team and we’ll be happy to turn it on.
Important: We use a non-standard SSH port. We'll provide the correct port number when SSH access is granted.
Once enabled, connect using an SSH client like this:
ssh youruser@yourdomain.com -p [port]
Replace [port]
with the port number we give you.
Once connected, enter the Node.js virtual environment using the command from the Web UI, e.g.:
source /home/youruser/nodevenv/node-scripts/20/bin/activate && cd /home/youruser/node-scripts
And you should then find that you can see Node.js at the required version:
node -v
v20.18.3
Using a dummy Node.js app is an effective workaround that allows you to run modern Node.js versions from the command line, even on servers running older operating systems where tools like NVM may not support newer versions due to system library limitations.
This method is ideal for CLI-based tasks such as:
npm
cron
You can also create multiple dummy apps if you want to use different Node.js versions for different projects.
Note:
Powered by WHMCompleteSolution