12
votes

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.

4

4 Answers

13
votes

I've only just started using pm2 and I found this helped.

You have your npm script to run es6 js, something like

{
  ...
  "scripts": {
    "server": "babel-node ./src/server.js"
  },
  ...
}

Then to run that with pm2 you use pm2 start npm -- run server Make sure you have babel-cli installed globally.

13
votes

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

With "@babel/cli": "^7.0.0" the path is ./node_modules/@babel/cli/bin/babel.js

0
votes

check pm2 log files to trace the error. the location of log files is at /root/.pm2/

0
votes

I fix this issue in my Node.js project that I use babel.

First you must have

"devDependencies": {
   "@babel/core": "^7.11.6",
   "@babel/node": "^7.10.5",
   "@babel/preset-env": "^7.11.5",
   "eslint": "^7.9.0"
}

in your package.json file.

And then you can use

pm2 start bin/www --interpreter node_modules/@babel/node/bin/babel-node.js

command on your terminal. That's it, your project will stand up with babel configurations. :)