For example, a usual directory structure will look like this:
├── src ├── dist └── package.json
src contains es6 , coffescript, or whatever and
But, when writing code, this’ll be harder to configure. Say that you want to watch and restart, you’ll have to watch, transpile, restart. Pm2 is not a build system nor a task runner, so prefer the second way.
This might work just fine on a production workflow, but I don’t advise it. Bundling code is safer, this will slower the script start process 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 CLI or the
exec_interpreter option through json configuration.
#- npm install -g coffee-script #- pm2 start --interpreter coffee index.coffee
--watch and you’ve a daemonized coffee script that’ll restart on file changes.
#- npm install -g babel-cli #- pm2 start --interpreter babel-node index.es6
Remember that those will only work with the
fork_mode, if you want to run a pm2 cluster, please see the alternative below.
require, or tweak the
module module so that the required script is transpiled before it’s interpreted (example babel or coffee).
This workaround could be considered more as a hack than an actual solution because of this. Keep it 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.