Node.js: Getting started
Deploy Node.js on Stackhero quickly and securely
👋 Welcome to the Stackhero documentation!
Stackhero delivers a ready-to-use Node.js cloud environment designed to help you move faster:
- Deploy to production in seconds with a simple
git push. No extra tooling or manual setup is required.- Bring your own domain and benefit from automatic HTTPS certificates, keeping your application secure without extra configuration.
- Automatic backups, one-click updates, and predictable pricing help you focus on shipping features, and Stackhero manages the infrastructure for you.
- Built on private, dedicated infrastructure for consistent performance and strong security.
Save time and reduce operational overhead: you can have your application running on Stackhero's Node.js cloud hosting in just a few minutes.
Deploying your Node.js service on Stackhero is designed to be fast, efficient, and reliable. This guide shows you how to get your app running in minutes, with built-in security and performance from the start.
Start a Node.js service
First, create a Node.js service on Stackhero. This gives your application a secure cloud environment and access to Stackhero's automated features.
Prerequisites
Before you begin, ensure you have the following:
- Git: Download from the official Git website.
- Windows users: For a smoother terminal experience, you can use Windows Terminal from the Microsoft Store.
Configure your service
The key setup step is adding your SSH public key. This allows Stackhero to securely access your repository for deployments.
To display your public key, you can run one of these commands in your terminal:
cat ~/.ssh/id_rsa.pub
or
cat ~/.ssh/id_ed25519.pub
If you do not have an SSH key pair, you can generate one with ssh-keygen on Linux or macOS, or ssh-keygen.exe on Windows.
Once you have your public key, log in to the Stackhero dashboard, select your Node.js service, open its configuration section, and paste your key into the provided field.
Tip: To use your SSH public key for all future services, you can set it globally. In the Stackhero dashboard, click your profile picture (top right), go to "Your profile", and paste your SSH public key there.
Clone the example
To make your first deployment even easier, Stackhero provides an example Node.js app. You can clone it with:
git clone https://github.com/stackhero-io/nodejsGettingStarted.git stackhero-nodejs-getting-started
cd stackhero-nodejs-getting-started
Configure the remote repository server
You can deploy your app via Git. On your service's main page, Stackhero provides a command to add a Git remote, similar to:
git remote add stackhero ssh://stackhero@<XXXXXX>.stackhero-network.com:222/project.git
You can copy and paste this command into your terminal to set up the remote.
Deploy your Node.js application
To deploy, push your code to Stackhero:
git push stackhero main
The first time you push, confirm the key fingerprint by typing "yes" if prompted.
Within seconds, your application is live. You can check its status by visiting the website URL shown on your Stackhero dashboard (for example, https://<XXXXXX>.stackhero-network.com).
Your application is now deployed.
To update your app, change your app.js file, commit, and push:
git add -A .
git commit -m "Update app.js"
git push stackhero main
Deploy an existing application
If you have an existing Node.js app, you can add the Stackhero remote and deploy your code:
git push stackhero main
Node.js version
Stackhero reads your Node.js version from your repository at each deployment. The version is determined in this order:
imagein yourstackhero.yml, for example:image: node:24-alpine3.21engines.nodein yourpackage.json, for example:"node": "24.x".nvmrc.node-version
If none are present, Stackhero uses the current Long Term Support (LTS) release. Using a range like 24.x ensures you always get the latest updates for that major version.
Full details on version pinning, available tags, and update behavior are in Node.js version.
Handle error "failed to push some refs to '[...]'"
If you see this error:
error: failed to push some refs to '[...]'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
(e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Your local repository is out of sync with the remote. You can force the push:
git push -f stackhero main
Handle error "src refspec main does not match any"
If you see this:
error: src refspec main does not match any
error: failed to push some refs to 'ssh://<XXXXXX>.stackhero-network.com:222/project.git'
This usually means the main branch does not exist. You can try pushing the master branch:
git push stackhero master
Handle error "Everything up-to-date" when pushing
If Git says "Everything up-to-date" but your changes are not deployed, you might have forgotten to commit. Try:
git add -A .
git commit -m "Your commit message"
git push stackhero main
To trigger a deployment without any changes, you can force an empty commit:
git commit --allow-empty -m "Force update"
git push stackhero main
Deploy a branch other than main
To deploy a branch other than main, such as production, you can run:
git push stackhero production:main
Deploy a tag
To deploy a specific tag (for example, v1.0), you can use:
git push stackhero 'v1.0^{}:main'
The ^{} ensures the commit referenced by the tag is pushed.
Roll back or deploy a specific commit
To deploy a specific commit, first get its hash with git log, then:
git push -f stackhero <HASH>:main
Deploy to multiple environments
To manage separate environments, such as production and staging, you can rename the current remote to stackhero-staging:
git remote rename stackhero stackhero-staging
Then create a new Node.js service in the dashboard and add it as stackhero-production:
git remote add stackhero-production ssh://stackhero@<XXXXXX>.stackhero-network.com:222/project.git
Now you can deploy to each environment with:
git push stackhero-production main
or
git push stackhero-staging main
Save your SSH private key password in macOS keychain
On macOS, if you are prompted for your SSH key password on every push, you can use the macOS keychain for convenience and security. For example:
/usr/bin/ssh-add --apple-use-keychain ~/.ssh/id_rsa
This command stores your key password so you are not prompted again during future deployments.
Handle secrets
When working with multiple environments, it is safest to use environment variables for secrets like tokens or passwords. This keeps sensitive data out of your repository.
You can manage environment variables in the Stackhero dashboard. Access them in your Node.js code like this:
process.env.mySecret
Open other network ports
If your application needs protocols beyond HTTP, you can open additional TCP or UDP ports directly in the Stackhero dashboard. Just specify the public entry port, the destination port on your Node.js service, and the protocol.
Store files
For user files such as photos, object storage is recommended. This approach lets you share files across services and instances and keeps your data separate from your code. MinIO is a high-performance, S3-compatible option.
If you prefer local storage, you can use the persistent storage at /persistent/storage/ on your Node.js instance.
CAUTION: Always store data inside the
/persistent/storage/folder.Data stored outside this folder may be lost if your instance reboots or when you push new code.
Graceful shutdown
When you deploy a new version, the previous app receives a termination signal so it can shut down cleanly. This lets your app close database connections or other resources.
The SIGTERM signal is sent to your app. You can handle it in your code:
process.on('SIGTERM', () => {
// This log appears in the Stackhero dashboard logs tab
console.info('SIGTERM signal received.');
// Close database connections and other services here
// ...
});
Run your code on multiple CPU cores
By default, Node.js uses a single core. To take advantage of all available CPU cores, you can use the Node.js cluster API. The official Node.js cluster documentation explains this in detail.
Here is a simple example:
const cluster = require('cluster');
const http = require('http');
const cpusCount = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers
for (let i = 0; i < cpusCount; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died`);
});
} else {
// Workers share any TCP connection, in this case, an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}