Managing and deploying Node.js applications
Starting a Node.js application
- mStudio UI
- CLI
- API
To start a Node.js application from the mStudio, follow these steps:
- Navigate to the project that you want to create the application in.
- Select the "Apps" menu item in the sidebar.
- Click the "Create App" button and select "Custom Node.js app".
- In the installation wizard, select the entrypoint command that should be used to start your application and complete the wizard.
After the installation has completed, observe the installation directory in the UI.
To start a Node.js application from the CLI, run the following command:
$ mw app create node --wait --entrypoint "npm start"
The --wait
flag will cause the CLI to wait until the installation has completed. The --entrypoint
flag will cause the CLI to configure the specified entrypoint command as the command that should be used to start your application. If omitted, the default entrypoint command yarn start
will be used.
After the installation has completed, use the mw app get
command to observe the installation directory.
To learn how to deploy a Node.js application via the API, read the article "Starting a Node.js application".
Deploying your app
After you have created your Node.js app, you can deploy your code by moving it into the designated application directory. You may use any method that you prefer to deploy your code, such as a local Git clone, rsync or SFTP.
Have a look at our collection of deployment guides for more information on how to deploy your code.
Making your app accessible via HTTP
See our guide on Running Webservers to learn how to make your Node.js app accessible via HTTP.
Managing your process lifecycle
See our guide on Running Webservers to learn how to manage the process lifecycle of your Node.js application.
Example applications
Express.js
The following code snippet shows a minimal example of an Express application that listens on the port specified in the PORT
environment variable:
const express = require("express");
const app = express();
const port = parseInt(process.env.PORT, 10) || 3000;
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
PM2 with live-reloading
To enable advanced features like live-reloading (useful for development) or clustering (when you want to use all available CPU cores), you can use the PM2 process manager.
For this, you need to create a Node.js app with pm2
as its entrypoint (or alternatively, configure pm2 start ecosystem.config.js --no-daemon
as the start
script in your package.json
file):
$ mw app create node --wait --entrypoint "pm2 start ecosystem.config.js --no-daemon"
Furthermore, you need to have PM2 installed in your application environment. You can install it via NPM:
$ npm install -g pm2
Next, create an ecosystem configuration file ecosystem.config.js
in your application directory:
module.exports = {
apps: [
{
name: "test-app",
script: "src/server.js",
watch: ["src"],
},
],
};
In this example, PM2 will start the src/server.js
script as the main application, and watch the src
directory for changes. If any changes are detected, PM2 will automatically restart the application. Have a look at the PM2 documentation reference for more information.
Using Yarn Plug'n'Play
If your project uses the Yarn PnP installation mode, it is recommended to disable the global cache.
enableGlobalCache: false
Environment Variables
Environment variables are generally used to configure Node.js applications. Depending on the Node.js version, we recommend different variants to define these.
- < Node.js 20.6
- >= Node.js 20.6
You can define environment variables in the file ~/.config/node/.env
. Variables defined there are then automatically available in your application.
As of Node.js 20.6, an (experimental) flag --env-file
is available (see Node.js documentation). This can be specified as often as required to load .env files whose environment variables are then available in the app.
To use the flag, configure the start command of your Node.js app as follows, for example:
node --env-file=.env server.js
The path to the .env
file is specified relative to the working directory.
If you use an npm or yarn script to start your application, the following example applies:
{
[..]
"scripts": {
"start": "node --env-file=.env server.js"
}
[..]
}