How to Correctly Install and Use Node.js in Shared Hosting Environments
**Description:**A comprehensive guide for Brixly shared hosting customers on installing npm, managing Node.js versions, and resolving common installation issues using cPanel and other available tools.
Table of Contents
- Introduction
- Prerequisites
- Setting Up a Node.js Application in cPanel
- Installing NPM Packages
- Managing Node.js Versions
- Practical Example: Hello World App
- Troubleshooting Common Issues
- Best Practices & Tips
- Further Resources
Introduction
Node.js is a powerful runtime environment for running server-side JavaScript applications. On Brixly shared hosting, deploying Node.js apps is straightforward with our cPanel interface, but there are some limitations and best practices you should know.
This guide covers:
- Setting up Node.js apps using cPanel controls
- Installing and managing npm packages
- Switching Node.js versions (where available)
- Resolving common installation and runtime issues
Prerequisites
Before you begin, ensure you have:
- An active Brixly shared hosting account with cPanel access
- Your Node.js application files ready (or a plan to start from scratch)
- Basic familiarity with web hosting and file management concepts
> Note: Shared hosting environments do not provide root/SSH access for system-wide installations. All Node.js and npm actions must be performed via cPanel or the provided terminal (where available).
Setting Up a Node.js Application in cPanel
- Log in to cPanel.
- Navigate to the Software section and click on Node.js App.
- Click Create Application.
- Application Root: Choose or create a folder (e.g.,
myapp
). - Application URL: Select your domain or subdomain.
- Application Startup File: Typically
app.js
orindex.js
. - Node.js Version: Select from the dropdown if multiple versions are offered.
- Application Root: Choose or create a folder (e.g.,
- Click Create.
Your application environment will now be set up. The system creates a virtual environment for your app, separate from other system processes.
Installing NPM Packages
To use npm packages in your application:
- In the Node.js App section, find your app and click Edit.
- Use the Run NPM Install button. This will install all dependencies listed in your
package.json
. - If you need to add a new package:
-
Update your
package.json
via File Manager or the cPanel Terminal (if available), or -
Use the application terminal in cPanel to run:
npm install <package-name> --save
-
- Click Restart Application after adding or updating packages.
> Tip: Always ensure your package.json
is in your app’s root directory.
Managing Node.js Versions
Brixly shared hosting often allows you to select from several Node.js versions:
- When creating or editing your application in the Node.js App section, use the Node.js Version dropdown to select your preferred version.
- Changing the version will require a restart of your application.
> Note: You cannot install custom Node.js versions in shared hosting. Use only the versions provided in cPanel.
Practical Example: Hello World App
-
Create Application Root
- In cPanel File Manager, navigate to your app root (e.g.,
myapp
).
- In cPanel File Manager, navigate to your app root (e.g.,
-
Create
index.js
const http = require('http'); const port = process.env.PORT || 3000; http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello, World!\n'); }).listen(port, () => { console.log(`Server running at port ${port}`); });
-
Set Startup File: Point the cPanel Node.js app to
index.js
. -
Start the App: Use cPanel controls to start/restart the application.
-
Test: Visit your application URL in a browser. You should see “Hello, World!”
Troubleshooting Common Issues
Application Not Starting
- Check startup file: Ensure the filename matches what’s set in cPanel.
- Missing dependencies: Use the NPM Install button or check your
package.json
. - Port errors: Always use
process.env.PORT
as the port—do not hard-code ports.
NPM or Package Errors
- Syntax/typo mistakes: Double-check your
package.json
and code files. - Unsupported packages: Some native modules requiring compilation may not work in shared hosting.
Node.js Version Issues
- Unsupported features: If you get syntax or runtime errors, check your selected Node.js version supports the features you’re using.
File Upload/Permission Issues
- Use File Manager: Upload and edit files using the cPanel File Manager to avoid permission problems.
- Max file size: Use FTP for files larger than 250 MB.
Application Crashes After Deployment
- Check logs: Use the View Logs feature in the Node.js App section for error messages.
- Restart app: Changes in code or dependencies often require a restart.
> If you have tried these steps and your app still does not work, please submit a support ticket with details and error messages.
Best Practices & Tips
- Keep dependencies up-to-date using npm.
- Do not use root or global npm installs—always install packages in your app directory.
- Restrict file permissions to enhance security.
- Backup your application before making significant changes.
- Monitor resource usage through cPanel to avoid exceeding shared limits.
Further Resources
- Official Node.js Documentation
- NPM Documentation
- Brixly Node.js App Setup Guide
- Troubleshooting Node.js Apps in cPanel
By following this guide, you’ll be able to confidently deploy, manage, and troubleshoot Node.js applications within your Brixly shared hosting environment.