# Migrating an existing Node.js application to the Node.js Selector in cPanel

> Move an existing Node.js app into the cPanel Node.js Selector and make the small code changes it needs to run there.

Source: https://helpcenter.ruachost.com/cpanel/software/migrating-an-existing-node-js-application-to-node-js-selector-in/

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

### 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

- Upload or copy your existing Node.js  application files into the **application root directory** you specified when creating the app.

### 3. Modify Script Files

Some adjustments may be required for compatibility with the Node.js  Selector:

- ### 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 `0` instructs Node.js  to assign an available port automatically.
- ### Import Directives

  - Some Node.js  versions in cPanel do not support `import`.
  - Replace `import` with `require`:

    ```
    const express = require('express');
    const path = require('path');
    const dotenv = require('dotenv');
    ```
- ### Routes

  - Update routes to include the application URL if required.

### 4. Install Dependencies

- Ensure your `package.json` file is in the application root.
- In the Node.js  Selector interface, click **Run NPM Install** to install dependencies.

### 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.

> **Note:**
- Migration usually requires only minor changes.
- Always restart the app after modifying files or dependencies.
- If you encounter errors, check logs and verify your `package.json` configuration.
