Migrating an Existing Node.js Application to Node.js Selector in cPanel
The Node.js Selector in cPanel allows you to run Node.js applications without manually configuring the server environment. If you already have a Node.js app, you can migrate it to the Node.js Selector with only minor modifications.
Steps to Migrate Your Application
Section titled “Steps to Migrate Your Application”1. Log in to cPanel
Section titled “1. Log in to cPanel”- Go to Tools → Software → Setup Node.js App.
- Click Create Application to set up a new environment.
2. Copy Application Files
Section titled “2. Copy Application Files”- Upload or copy your existing Node.js application files into the application root directory you specified when creating the app.
3. Modify Script Files
Section titled “3. Modify Script Files”Some adjustments may be required for compatibility with the Node.js Selector:
-
Port Number
Section titled “Port Number”-
If your app uses a hard‑coded port (e.g.,
3000), change it to obtain a port dynamically. -
Example:
const express = require('express');const app = express();app.get('/myapp', (req, res) => res.send('Hello World!'));const server = app.listen(0, () => {console.log('App listening at http://localhost:' + server.address().port);});Using
0instructs Node.js to assign an available port automatically.
-
-
Import Directives
Section titled “Import Directives”-
Some Node.js versions in cPanel do not support
import. -
Replace
importwithrequire:const express = require('express');const path = require('path');const dotenv = require('dotenv');
-
-
Routes
Section titled “Routes”- Update routes to include the application URL if required.
4. Install Dependencies
Section titled “4. Install Dependencies”- Ensure your
package.jsonfile is in the application root. - In the Node.js Selector interface, click Run NPM Install to install dependencies.
5. Restart and Test
Section titled “5. Restart and Test”- Restart the application from the Node.js Selector dashboard.
- Visit the Application URL to confirm it runs correctly.
- Use Error Logs in cPanel to troubleshoot issues.