Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forever errors with babel-node

I have a simple node server:

//server.js

import express  from 'express';
import React    from 'react';
...

When I try to run this using Forever:

forever start -c "babel-node --experimental" server.js , it errors out due to use of import

/Applications/MAMP/htdocs/React/ReactBoilerplates/koba04/app/server.js:1
(function (exports, require, module, __filename, __dirname) { import express  
                                                              ^^^^^^
SyntaxError: Unexpected reserved word
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3
error: Forever detected script exited with code: 8

I have also tried pm2 and nodemon, I get same error there as well. For pm2, I followed this issue https://github.com/Unitech/PM2/issues/1167, but it didn't work either. What am I doing wrong here?

like image 810
a_rahmanshah Avatar asked Jun 16 '15 08:06

a_rahmanshah


2 Answers

forever start -c "node -r babel-register" ./src/index.js

Also works.

like image 64
basickarl Avatar answered Oct 20 '22 00:10

basickarl


This works for on-the-fly transpilation for me: forever start -c node_modules/.bin/babel-node server.js

Another solution is using the Require Hook like this:

// server-wrapper.js
require('babel/register');

require('./server.js');

Then run forever start server-wrapper.js.

like image 28
oskarth Avatar answered Oct 20 '22 01:10

oskarth