To start a TypeScript application using ts-node
with PM2, you can use an ecosystem configuration file (ecosystem.config.js
) or directly pass the command to PM2 using the --interpreter
flag. Here's how you can do it using both methods:
- Create an
ecosystem.config.js
file in your project root with the following content:
module.exports = {
apps: [
{
name: "MQTT Subscriber",
script: "./dist/src/main.js", // Point to the compiled JavaScript file
},
],
};
- Start the application with PM2 by running:
pm2 start ecosystem.config.js
This method allows you to easily manage multiple applications and their configurations.
Alternatively, you can start your application directly with PM2 without creating an ecosystem.config.js
file, using the --interpreter
flag:
pm2 start node_modules/.bin/ts-node --interpreter none -- ./src/main.ts
Here, you're telling PM2 to use ts-node
as the script to run, with none
as the interpreter (meaning it should just execute the script directly), and then specifying the path to your TypeScript file.
- Ensure
ts-node
andtypescript
are installed in your project. If not, you can install them using npm or yarn (npm install ts-node typescript --save
). - The
--interpreter none
option is used to tell PM2 not to prepend the command withnode
. Sincets-node
is an executable that runs your TypeScript files, you don't need Node.js to run it explicitly. - If you're using PM2 in a production environment, it's a good practice to compile your TypeScript to JavaScript and run the compiled code directly with Node.js. This approach improves performance and stability.
- Verify the Application is Running: You can verify that your application has started successfully by running:
pm2 list
This command displays a list of all applications currently managed by PM2, including their status, CPU, and memory usage.
- Logs and Monitoring: If you need to check the logs for your application, you can use:
pm2 logs subscriber
This command shows the real-time logs for the "subscriber" application. You can also monitor your application in real-time using:
pm2 monit
- Save the PM2 List: To ensure that your application restarts automatically after a reboot, you can save the current list of applications managed by PM2:
pm2 save
- Setup Startup Script: Finally, to automate the restart of your application upon system reboot, you can generate and execute a startup script provided by PM2:
pm2 startup
Follow the instructions provided by the pm2 startup
command to set up PM2 to automatically start your application when your system boots up.
These steps should help you successfully deploy your application using PM2 with your pm2-subscriber-conf.json
configuration file.
for future updates use:
alias update_mqtt='cd ./mqttx-subscriber/ && git pull && npm run build && pm2 restart 3'