14

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
jsDevia
  • 1,282
  • 4
  • 14
  • 37

7 Answers7

14

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.

Moosch
  • 159
  • 1
  • 6
13

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

techBeginner
  • 3,792
  • 11
  • 43
  • 59
Peter
  • 4,493
  • 6
  • 41
  • 64
3

use this command to run a project

pm2 start --interpreter babel-node server.js
1

pm2 start --interpreter babel-node server.js

use this command to run a project on to the server

0

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

TheEhsanSarshar
  • 2,677
  • 22
  • 41
0

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. :)

crazycoder
  • 39
  • 6
0

I was reading this I found something related to this point "Error: spawn babel-node ENOENT" I think the problem isn't with pm2, It is related to the path, I mean the problem is with your environment!

well, how to fix it!

first, let me be really clear about this, I have been working with pm2 for about 4 years and it is the best in my opinion, but in windows environment it is creasy! soo my solution to not lose time, is nodemon for windows, if you are using Linux "WALA nop problems",

my way! in my server(Linux or Ubuntu) for production or testing, I always use pm2 and as a dependence, I install nodemon just in case my team wants to use it on windows!

example babel pm2 nodemon

**red lines are for Linux and green for Windows **

another way to use is to install ubuntu on windows from WINDOWS STORE

enter image description here

This allows you to run a Linux terminal on windows but you have to install nodejs on your local instance!

this example was for a friend how was losing time loving pm2, he is using this way and he is happyYYYYY

Daniel
  • 373
  • 4
  • 10