How to use babel-node with pm2

jsDevia picture jsDevia · Feb 6, 2018 · Viewed 11.9k times · Source

I want to manage my node process with pm2 package. If I don't have any es6 syntax in my code so I don't need to add babel-node, and without any es6 syntax code is am able to run my code with pm2 with this line of code

pm2 start server.js

But as soon as I add any line of code of es6 syntax like this

import express from 'express';

I get the error of unexpected token import.

As you know to solve this issue we have to add babel-node package.

But when I use this line of command to compile my code

pm2 start server.js --interpreter babel-node

I get this error

Error: spawn babel-node ENOENT
at _errnoException (util.js:1022:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)

The strange thing about this is that if I don't use pm2 and run the code with this line of code

babel-node server.js

Every thing is OK.

Answer

Peter picture Peter · Jun 20, 2018

Install babel-cli in your project with yarn add babel-cli and then you can run pm2 start ./server.js --interpreter ./node_modules/babel-cli/bin/babel-node.js