Migrating an Existing Node.js Application
Knowledgebase Article
}
Knowledgebase Article
If you're moving a Node.js application that was originally developed on your local machine or on a VPS, you’ll likely need to make a few small adjustments before it will run successfully under cPanel’s Node.js Selector.
This is because the Node.js Selector runs your app within a managed hosting environment that uses Phusion Passenger to handle both routing and process management. Unlike local setups where your app may run its own web server and binds to a custom port like 3000, this environment:
npm start
script — it uses the startup file you define in cPanelAdditionally, some apps written using ES module syntax (import/export
) may need changes depending on the Node.js version in use and how the app is structured.
This guide will walk you through how to adapt your existing app to work in this environment — including setting it up correctly, modifying the startup script if needed, and handling route paths if you're deploying under a subdirectory.
By the end, your app should run smoothly under the Node.js Selector, just like it did locally or on your previous host.
Even if you have all your files ready to go:
app.js
or server.js
. The startup script you define here must match the actual filename of your app’s main entry point.This ensures your environment is ready before uploading code, avoiding conflicts or permission errors.
Once the app is created:
package.json
.Most apps developed elsewhere will:
import
) instead of require
You must not use app.listen(3000)
or any other port in your code under Passenger.
Instead, you have two options:
This is the recommended and cleanest approach:
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello World!'));
// Don't listen! Just export.
module.exports = app;
Passenger will automatically start your app and connect incoming requests to it.
If you absolutely need to call app.listen()
(e.g. the framework expects it), you can tell Node to choose a port dynamically:
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello World!'));
const server = app.listen(0, () => {
console.log('App running on port', server.address().port);
});
0
tells Node.js to pick any available port, which satisfies Passenger without causing conflicts.
If your app uses ES module syntax (e.g. import express from 'express';
), and you encounter errors:
Replace import
with require
:
// Old:
import express from 'express';
// New:
const express = require('express');
This change improves compatibility across different Node.js versions.
package.json
file in the root directory (the one you set as the Application Root in cPanel). This file lists the packages (like Express) that your app depends on.package.json
into your app's private environment.If your application is configured to run from a subdirectory — for example, https://example.com/myapp
— then your routes must match that structure.
This is because the Node.js Selector mounts your application at the exact URL path you define in the Application URL field. It doesn’t treat /
as the root path by default — it treats /myapp
as the entry point.
If your route is written like this:
app.get('/', (req, res) => res.send('Hello!'));
It will only respond to requests at https://example.com/
— but if your app is mounted at https://example.com/myapp
, this route will not be reached. Instead, you'll see an error like:
Cannot GET /myapp
You can rewrite your routes to include the subdirectory:
app.get('/myapp', (req, res) => res.send('Hello from myapp!'));
app.get('/myapp/test', (req, res) => res.send('This is /test'));
A cleaner way is to use a router and mount it at the subdirectory path:
const express = require('express');
const app = express();
const router = express.Router();
router.get('/', (req, res) => res.send('Hello from root!'));
router.get('/test', (req, res) => res.send('This is /test'));
app.use('/myapp', router);
This keeps your routing code clean and avoids repeating /myapp
throughout.
Powered by WHMCompleteSolution