For example, a usual directory structure will look like this:
├── src ├── dist └── package.json
src contains es6 , coffescript, or whatever and
However, when writing code, it might make it harder to configure. Say that you want to watch and restart. You will have to watch, transpile, restart. PM2 is not a build system nor a task runner, so we advise you to prefer the second way.
This might work just fine on a production workflow even though we would not recommend it. Bundling code is safer, this will make the script start process slower and the cluster mode might not be available.
The easiest way to use transpilers with PM2 is to override the execution interpreter (
exec_interpreter). Note that if this is changed, your code will only work in
fork_mode (check here for differences between fork modes).
To do so, specify the
--interpreter option through the CLI or the
exec_interpreter option through json configuration.
#- npm install -g coffee-script #- pm2 start --interpreter coffee index.coffee
--watch to have a daemonized coffee script that will restart on file changes.
#- npm install -g babel-cli #- pm2 start --interpreter babel-node index.es6
Remember that those commands will only work in
fork_mode. If you want to run a PM2 cluster, please refer to the alternative below.
require, or tweak the
module so that the required script gets transpiled before it is interpreted (example babel or coffee).
This workaround could be considered more as a hack than an actual solution. Keep in mind that it will slow down the script startup.
# server.js require('coffee/register'); require('./server.coffee');
Check out the babeljs documentation for more options.
Then, all you have to do is start the script
pm2 start server.js. As this will use the
node interpreter, the cluster mode will work as expected.